Hey guys.

I need help on how to set a specific element that I insert into my vector of bool type :S

set::set()
{
     cardinality = 0;
     setlist.resize(DEFAULTSIZE, false);
}

void set::insert(int x)
{
     if (x >= 0 && x <= DEFAULTSIZE)
     {
           setlist.insert(setlist.begin(), 1, x);
           x = true;
           cardinality++;
     }  
}

void set::dump() const
{
     for (int i = 0; i < DEFAULTSIZE; i++)
     {
         if (setlist.at(i) == true)
         {
            cout << i << " ";
         }
     }
}

Okay, so when I create the set, all elements of DEFAULTSIZE ie. 100 elements are all set as false by default.

But I want my insert(int x) function to insert a value x, but set it to true so that my dump() function can only display the values set as "true" in the set created.

Any help would be appreciated, because I tried doing x = true after the setlist.insert function but it still does not set x as true.

Thanks :)

Edited 7 Years Ago by scantraXx-: n/a

i guess u r looking for something like

struct myData
{
    int data;
    bool valid;
}

vector<myData>   setList;

Edited 7 Years Ago by dkalita: n/a

EDIT: Never thought of using structs. I'll see how it goes.

Thanks :)

Edited 7 Years Ago by scantraXx-: n/a

Hmm okay. Can you please explain what int data is used for?

It is the "int x" that u have used in your code.
I just gave it a different name.
(NB. Use meaningful variable name in your program. This makes it more meaningful when someone sees the code.)

I get this error when compiling:

expected init-declarator before "setlist"

for that u have to post your code so that I can see where the error occured...........

Edited 7 Years Ago by dkalita: n/a

Ahh I see now. okay thanks.

Got the error fixed too :)

u welcome.
hope u can mark this thread as solved i guess.... :)

Wait, this can't work because I am not allowed to change my vector <bool> setlist because it's a private data field in my class.

Did you mean for me to change my bool vector to a stuct vector?

Edited 7 Years Ago by scantraXx-: n/a

Wait, this can't work because I am not allowed to change my vector <bool> setlist because it's a private data field in my class.

Did you mean for me to change my bool vector to a stuct vector?

yep

It's a private field which I can't change.. and my teacher won't let me :( So I think we need another way around this :S

i guess u r looking for something like

struct myData
{
    int data;
    bool valid;
}

vector<myData>   setList;

slight modification to it

typedef struct
{
    int data;
    bool valid;
} myData;

vector<myData>   setList;

It's a private field which I can't change.. and my teacher won't let me :( So I think we need another way around this :S

then i think u can consider 0(zero) as an invalid value.

This article has been dead for over six months. Start a new discussion instead.