C++ trouble erasing elements from a <vector>

  • #1

Main Question or Discussion Point

Hi, I've been having trouble erasing elements from a <vector> (that is from the STL of C++)

See I declared 2 vectors V and S, V contains lets say all the elements, while S is a solution set which contains some elements that are classified... well yeah....

I need to create V-S which is a vector that has all the elements in V that are not in S.. I was planning on doing this by making a copy of V into V_S and then erasing every element I add to S from V_S.

sort of like this:

S.push_back(x);
V_S.erase(x);

I get an error... Why?
 

Answers and Replies

  • #2
437
1
What kind of error do you get? Compilation error?
I would suggest you to iterate with a loop through all the elements of V_S and check with a vector method (I think called exists()) if each element also exists in vector S. If yes just remove them.
 
  • #3
591
0
You have to pass an iterator to erase, not the element you want to remove.
 

Related Threads on C++ trouble erasing elements from a <vector>

  • Last Post
Replies
6
Views
3K
  • Last Post
Replies
3
Views
3K
  • Last Post
2
Replies
28
Views
11K
Replies
1
Views
3K
Replies
1
Views
766
  • Last Post
Replies
9
Views
2K
  • Last Post
Replies
23
Views
2K
  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
15
Views
6K
  • Last Post
Replies
2
Views
2K
Top