how i can pass inside a vector an array?

i know how to do it without classes but i don't know how to do it with constructor.

i try to create a 2nd constructor that takes array of integers and the size of array and initialize a vector.

header

#ifndef INTEGERSET_H
#define	INTEGERSET_H
#include <vector>
using namespace std;

class IntegerSet
{
public:
    
    IntegerSet();
    IntegerSet(); //second constructor

    IntegerSet unionOfSets(IntegerSet &,IntegerSet &);
    IntegerSet insertSectionOfSets(IntegerSet &,IntegerSet &);

    void insertElement(int);
    void deleteElement(int);
    void printSet() const;
    
    bool isEqual(IntegerSet &);

private:
    vector <bool> elements;
};

#endif	/* INTEGERSET_H */

and in cpp

IntegerSet::IntegerSet()
{
    elements.resize(100);
}

IntegerSet::IntegerSet()
{
    //how i fix this one?
}

Recommended Answers

All 3 Replies

i did this but not sure if its what the exercise asks

IntegerSet::IntegerSet(int a[],int size)
{
    elements.resize(size);
    
    for(int x=0;x<=size;x++)
        elements[x]=a[x];
}

>>i did this but not sure if its what the exercise asks

That looks OK to me, except that your vector should be a vector of integers if you plan to store integers. That is:

vector<int> elements;  //not vector<bool> elements;

Another idiomatic way to fill that vector is using the assign() function, as so:

IntegerSet::IntegerSet(int a[],int size)
{
    elements.assign(a, a + size);  //picks the elements from a[0] to a[size-1].
}

Or, better yet, if you have learned about initialization lists in constructors, then you can do:

IntegerSet::IntegerSet(int a[],int size) :
                       elements(a, a + size) //initializes "elements" with values from a[0] to a[size-1].
{ /* nothing to do here */ }

thanks a lot,the exercise is with bool,so its ok

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.