remove items from a vector using iterator

H

happyvalley

Hi,
I want to remove some elements from a vector, the following code
doesn't work, seems it doesn't allow me to remove an element when
iterating the vector. (make sense), just wonder, how to do this?
thank

vector<int> IntVec;
vector<int>::iterator intIterator;

for(int i=0; i<10;i++) IntVec.push_back(i);
for(int i=0; i<10;i++) IntVec.push_back(i);
for(int i=0; i<10;i++) IntVec.push_back(i);

for(intIterator = IntVec.begin(); intIterator !=IntVec.end();
intIterator++)
{
if(*intIterator == 2)
IntVec.erase(intIterator);
}
 
V

Victor Bazarov

happyvalley said:
I want to remove some elements from a vector, the following code
doesn't work, seems it doesn't allow me to remove an element when
iterating the vector. (make sense), just wonder, how to do this?
thank

vector<int> IntVec;
vector<int>::iterator intIterator;

for(int i=0; i<10;i++) IntVec.push_back(i);
for(int i=0; i<10;i++) IntVec.push_back(i);
for(int i=0; i<10;i++) IntVec.push_back(i);

for(intIterator = IntVec.begin(); intIterator !=IntVec.end();
intIterator++)

Drop the increment from the 'for' (and next time don't use post-
increment, use pre-increment).
{
if(*intIterator == 2)
IntVec.erase(intIterator);

Replace those with

if (*intIterator == 2)
intIterator = IntVec.erase(intIterator);
else
++intIterator;

and get yourself a decent book on the Standard library.

V
 
N

NM

Victor said:
Drop the increment from the 'for' (and next time don't use post-
increment, use pre-increment).


Replace those with

if (*intIterator == 2)
intIterator = IntVec.erase(intIterator);
else
++intIterator;


and get yourself a decent book on the Standard library.

V

thanks for the answer and your advice, have another general question
for you, just wonder does an experienced programmer always have some
reference on hand, I have been coding in different languages, c++,
java, ... but I know a task is possible in a language and know the
general idea, but always don't remember the code in detail,
just wonder if this is the case for you. or ...

thanks
 
D

Daniel T.

NM said:
have another general question for you, just wonder does an
experienced programmer always have some reference on hand, I have
been coding in different languages, c++, java, ... but I know a task
is possible in a language and know the general idea, but always
don't remember the code in detail, just wonder if this is the case
for you. or ...

Have a reference on hand, at least have a few websites that you trust.
That said, when someone is coding 40+ hours per week in the same
language for years, it's unlikely he will need the same sort of
reference as someone who just learned the language.
 
V

Victor Bazarov

NM said:
[..] have another general question
for you, just wonder does an experienced programmer always have some
reference on hand, I have been coding in different languages, c++,
java, ... but I know a task is possible in a language and know the
general idea, but always don't remember the code in detail,
just wonder if this is the case for you. or ...

It is, and it isn't... Depends.

I use online reference most of the time; on Windows it's MSDN, which
has got much better along with their compiler. Unfortunately I have
less time now to read books, which contain much more of "how-to" type
of advice, which mostly is absent from references. To see how some
language constructs should work, I use the Standard document, which
is sometimes easier to browse than MSDN. Reading all that stuff
makes you analyze the connections between areas, and the solutions
just stick with you.

Working on a large project in a team with more than 1 person has its
advantages that you can always either ask somebody or look in the
existing code since most of it has already been used/implemented.
Just having to fix some code forces you to dig into it and understand
what's going on, and that's the other source of "how things should
be done". After some number of years working on different parts of
different applications in different fields, you accumulate a certain
amount of experience that allows you not to have to look solutions
up, but rather make them up from small pieces floating on the surface
of your memory.

Reading this newsgroup and posting to it is a constant exercise of
one's "library of tricks". I participate here as much as my time
allows (or perhaps a bit more than that), but it does give me some
edge when it comes to the language.

V
 
W

William

vector is 'random-access' container,
so if you remove an element by iterator, the iterator
will invalidate.
you had better remove by index.
 
B

Bo Persson

William said:
vector is 'random-access' container,
so if you remove an element by iterator, the iterator
will invalidate.
you had better remove by index.

No need at all for that.

Victor just showed below that vector::erase returns a new valid iterator for
the element after the removed one.

intIterator = IntVec.erase(intIterator);

is perfectly valid code.


Bo Persson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top