C++ trouble erasing elements from a <vector>

In summary, The person is having trouble erasing elements from a vector in C++. They have declared two vectors, V and S, with V containing all elements and S containing some elements that are classified. They need to create V-S, a vector with all elements from V that are not in S. They planned on making a copy of V called V_S and erasing elements added to S from V_S. They received an error and were suggested to iterate through V_S and use a vector method to check if each element exists in S before removing it. It is noted that an iterator should be passed to the erase function, not the element itself.
  • #1
FrostScYthe
80
0
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 let's 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?
 
Technology news on Phys.org
  • #2
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
You have to pass an iterator to erase, not the element you want to remove.
 

What is a vector in C++?

A vector in C++ is a container that allows for dynamic arrays. It can resize itself to accommodate new elements and provides efficient random access to its elements.

How do I erase elements from a vector in C++?

To erase elements from a vector in C++, you can use the erase() function. This function takes in two parameters: an iterator pointing to the element to be erased and an iterator pointing to the element after the one to be erased. Alternatively, you can also use the pop_back() function to remove the last element of the vector.

Why am I having trouble erasing elements from a vector in C++?

There could be multiple reasons for having trouble erasing elements from a vector in C++. Some common issues include using an invalid iterator, trying to erase an element that has already been erased, or trying to access elements beyond the bounds of the vector. Make sure to double check your code and debug any errors that may be occurring.

How do I avoid memory leaks when erasing elements from a vector in C++?

One way to avoid memory leaks when erasing elements from a vector in C++ is to use smart pointers, such as unique_ptr or shared_ptr, instead of raw pointers. These smart pointers have built-in memory management capabilities and will automatically free up the memory when the vector is resized or destroyed.

Can I erase multiple elements from a vector in C++ at once?

Yes, you can erase multiple elements from a vector in C++ at once by using the erase() function with a range of iterators. This range specifies the elements to be erased, and the function will remove all the elements in that range.

Similar threads

  • Programming and Computer Science
Replies
23
Views
2K
Replies
13
Views
2K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
4
Views
833
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
Replies
2
Views
447
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
3
Views
1K
Replies
15
Views
1K
Back
Top