• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

STL question about vector

Having become somewhat of a Java guru, I have come to hate STL. Regardless I have to use it.

Now, regarding the STL vector, how do I go about iterating through the vector and removing items as I iterate?

In Java it is easy, all you do is call iter.remove(). Done. STL can not be so simpel though. So, what do I have to do to go through a vector and remove them (depending on criteria) as I go through them?

It's a small set so efficiency is a non issue.

Also, are there any other STL collections I should use? The 'list' is a linked list which would be more efficient at removing items mid-list. If I went this route, is it easier to go through the list and remove tiems on a case by case basis?

EDIT: If anyone could go through and write some code for me, that would be awesome. Something like this:


OK.... why is that code not formatted properly? SHould have looked like this:
list::iterator iter = someList.iterator();
for (;iter != someList.end(); iter++) {
Foo foo = *(iter);
if (foo.getX() == 5) {
// TODO: remove foo
}
}
 
vec.erase(std::remove(vec.begin(), vec.end(), value_to_remove), vec.end());

or

vec.erase(std::remove_if(vec.begin(), vec.end(), functor), vec.end());

Or if you really want to do it in a loop:

while(iter != vec.end())
{
if(removal check) iter = vec.erase(iter);
else ++iter;
}

Or you can reverse your loop (go from end - 1 to begin, or rbegin to rend), since erase only invalidates iterators after the erased element.

However, if you need to add and remove to the middle of a container, a vector (array) is probably not a good choice. Look at list, set or multiset, depending on your needs. Those also have different iterator invalidation behavior (only iterators to the erased element are invalidated).

I have found STL to be considerably more elegant, more powerful and better designed than Java collections. If you intend on doing anything non-trivial in C++, you may want to look into picking up a good STL book, like Scott Meyers' Effective STL. Effective C++ and More Effective C++ are also excellent books.
 
Venis,

A big thank you. I saw a similar solution to how you wrote your loop and questioned it. I think I'll go that route....

while(iter != vec.end())
{
if(removal check) iter = vec.erase(iter);
else ++iter;
}

EDIT: Well, I got it to compile. next stop ... testing.
 
I agree that STL's syntax isn't all that elegant at times. At least it doesn't require language extensions. TBB follows the same procedure -- nasty templatized syntax, no compiler changes.
 
TBB? What is TBB? Threaded building blocks?

STL ... non-standard library. Works as long as there is a human in the loop. I say this because no two implementations of STL are the same.
 
Back
Top