0

Following is my code:

//adds element j into the same set as element i
void addelem(int j, int i, set<set<int>> s)
{
    for (auto itr1 = s.begin(); itr1 != s.end(); ++itr1) {
        for (auto itr2 = (*itr1).begin(); itr2 != (*itr1).end(); ++itr2) {
            if (*itr2 == i) {
                (*itr1).insert(j);
            }
        }
    }

}

But I'm getting an error in this line:

(*itr1).insert(j);

Error reads:

no instance of overloaded function matches argument list C++

HotFondue
  • 28
  • 4
  • Contents of a set cannot be modified, they are effectively `const`. You are attempting to modify a value in the set (by modifying the set that's in the "outer" set), which is not allowed. See the duplicate question for more information. – Sam Varshavchik Oct 24 '20 at 14:27
  • @SamVarshavchik Understandable, but in my case, I cannot erase it / temporarily store its contents elsewhere. Is there any way around this? – HotFondue Oct 24 '20 at 14:35
  • "cannot erase it / temporarily store its contents elsewhere" why not? – 463035818_is_not_an_ai Oct 24 '20 at 14:36
  • @idclev463035818 because as you can see above, I am implementing it on a set of sets, and I get that error if I try to use .erase() or .swap() with my iterator variables. – HotFondue Oct 24 '20 at 14:44
  • 1
    you have to remove the inner set from the outer one before you can modify it and then insert it again – 463035818_is_not_an_ai Oct 24 '20 at 14:46
  • @idclev463035818 ok, could you give me an idea of what function or method I could use to do that? – HotFondue Oct 24 '20 at 14:55
  • @Zainab its explained in the duplicate. You have a `set>` and you want to modify one element of the `set`, so you have to first remove that set from the `set>` then remove the `int` from that `set` and then insert them back. Maybe you are confused because currently you are focusing on the `set` only – 463035818_is_not_an_ai Oct 24 '20 at 14:57
  • No, there's no simple "way around this". This is how `std::set` works. Whatever you're trying to accomplish here, you will need to find some other way to do it. – Sam Varshavchik Oct 24 '20 at 15:03
  • @SamVarshavchik I see. Appreciate the input. – HotFondue Oct 24 '20 at 15:42

0 Answers0