-1

Segfaulting code

Hey all, was just wondering how to delete elements from a set if its detected in another set. The current code iterates through both sets using a for loop, then if the value the iterators hold is the same, it attempts to erase from the first set.

It would look something like this:

#include <iostream>
using namespace std;
#include <vector>
#include <string>
#include <set>



int main() {
set<int> myset;
set<int> myset2;

for (int i = 0; i < 10; i++) {
    myset.insert(i);

}
for (int i = 0; i < 5; i++) {
    myset2.insert(i);
}

for (auto iter = myset.begin(); iter != myset.end(); iter++) {
    for (auto iter2 = myset2.begin(); iter2 != myset2.end(); iter2++) {
        if (*iter == *iter2) {
            myset.erase(*iter);
        }
    }
    
}

for (auto it = myset.begin(); it != myset.end(); it++) {
    cout << *it;
}
}
  • 1
    All questions here should have all relevant information ***in the question itself as plain text***. Links can stop working at any time making questions meaningless. Code, data, or errors shown as images cannot be copy/pasted; or edited or compiled for further research and investigation. Can you [edit] this question, removing and replacing all links and images with all relevant information as plain text? All code must meet all requirements of a [mre]. You'll find many other questions here, with a [mre], in plain text. Please use them as an example for how your question should look. – Sam Varshavchik Oct 07 '22 at 20:14
  • 1
    I really don't understand why so many new posters believe that taking pictures is easier than simply posting text in the edit window. Am I missing something, or is this the way some other websites present code (as pictures), and that habit has been brought over here? – PaulMcKenzie Oct 07 '22 at 20:18
  • *t would look something like this* -- Post the whole code, not a section of it. – PaulMcKenzie Oct 07 '22 at 20:20
  • posted whole code – astreadwell Oct 07 '22 at 20:22
  • 1
    No, this is not the whole code. My C++ compiler can't compile it, if I cut/paste what's shown into a new empty file. That might be because of missing header files. Before posting their first question on stackoverflow.com, everyone should take the [tour], read the [help], understand all the requirements for a [mre] and [ask] questions here. Not doing any of this results in a poor quality question almost every time. It then gets downvoted, closed, and then deleted. Repeated low-quality questions may result in a temporary ban from asking new questions. – Sam Varshavchik Oct 07 '22 at 20:24
  • @astreadwell [See this](https://godbolt.org/z/aoKqxarnz). That's what you gave us. See the compiler errors? Now, get that code to compile over at godbolt without error. Once you do that, come back to the question and post *that* program. – PaulMcKenzie Oct 07 '22 at 20:26
  • okay i added the #includes and the namespace std; I checked on godbolt and it didnt error – astreadwell Oct 07 '22 at 20:27
  • 1
    https://en.cppreference.com/w/cpp/container/set/erase "References and iterators to the erased elements are invalidated. Other references and iterators are not affected." So what happens if you try to increment an iterator that's already been invalidated? Probably something bad like a segfault. – George Oct 07 '22 at 20:29
  • https://stackoverflow.com/questions/283977/c-stl-set-difference for a full answer to set diffs – dcsuka Oct 07 '22 at 20:34
  • You don't need a nested `for` loop to do this. A set does not store duplicates – PaulMcKenzie Oct 07 '22 at 20:35
  • I tried to debug your code, but my IDE can't extract code from images. No code pasted as text == no help. – Thomas Matthews Oct 07 '22 at 20:35
  • @George thank you. That makes sense. I think I'll have to try a different method. – astreadwell Oct 07 '22 at 20:35

1 Answers1

3

The simple solution is to use one for loop.

for (auto val : myset2)
   myset.erase(val);

and not the doubly-nested for loop you're using now.

The std::set::erase can erase by key or iterator. All you need to do is provide the key element in this case.

Full example.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45