C ++ set vector gives segmentation fault after doing push_back

I have created a vector of sets in my program and I need to go through each of the sets. If a specific element is found in the set, I need to add a new set to the vector. However, this gives me a segmentation fault as soon as my array counter reaches the elements that I inserted later (inside the loop). In the following code, including list.push_back (cS) gives me a segmentation fault.

int main(void)  {
set<int> cS;
vector<set<int> > list;

cS.insert(1);
list.push_back(cS);

cS.insert(2);
list.push_back(cS);

for (int ctr = 0; ctr < list.size(); ctr++)
{
    for (set<int>::iterator itr = list[ctr].begin(); itr != list[ctr].end(); itr++)
    {
        if (*itr == 1 || *itr == 2)
        {
            cS.clear();
            cS.insert(3);
            //list.push_back(cS);
        }
    }
}

for (int ctr = 0; ctr < list.size(); ctr++)
{
    for (set<int>::iterator itr = list[ctr].begin(); itr != list[ctr].end(); itr++)
    {
        cout << *itr << endl;
    }
}

return 0;
}

      

I would be grateful if anyone can explain why this is giving an error (in gcc).

Thanks for visiting my post.

+3


source to share


1 answer


When you are push_back

in your vector, you will invalidate all references to elements in it in the event that the vector needs to allocate more memory. In your case, the iterator itr

becomes invalid after push_back

. One solution would be to add the sets to a separate list (vector) and then add them right after the for loop:



vector<set<int> > add;
for (int ctr = 0; ctr < list.size(); ctr++)
{
    for (set<int>::iterator itr = list[ctr].begin(); itr != list[ctr].end(); itr++)
    {
        if (*itr == 1 || *itr == 2)
        {
            cS.clear();
            cS.insert(3);
            add.push_back(cS);
        }
    }
}
list.insert(list.end(), add.begin(), add.end());

      

+4


source







All Articles