Connecting Tech Pros Worldwide Forums | Help | Site Map

remove items from a vector using iterator

happyvalley
Guest
 
Posts: n/a
#1: Dec 21 '06
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<intIntVec;
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);
}


Victor Bazarov
Guest
 
Posts: n/a
#2: Dec 21 '06

re: remove items from a vector using iterator


happyvalley wrote:
Quote:
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<intIntVec;
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).
Quote:
{
if(*intIterator == 2)
IntVec.erase(intIterator);
Replace those with

if (*intIterator == 2)
intIterator = IntVec.erase(intIterator);
else
++intIterator;
Quote:
}
and get yourself a decent book on the Standard library.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


NM
Guest
 
Posts: n/a
#3: Dec 21 '06

re: remove items from a vector using iterator



Victor Bazarov wrote:
Quote:
happyvalley wrote:
Quote:
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<intIntVec;
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).
>
Quote:
{
if(*intIterator == 2)
IntVec.erase(intIterator);
>
Replace those with
>
if (*intIterator == 2)
intIterator = IntVec.erase(intIterator);
else
++intIterator;
>
Quote:
}
>
and get yourself a decent book on the Standard library.
>
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
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

Daniel T.
Guest
 
Posts: n/a
#4: Dec 21 '06

re: remove items from a vector using iterator


"NM" <nittanymountain@gmail.comwrote:
Quote:
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.
Victor Bazarov
Guest
 
Posts: n/a
#5: Dec 21 '06

re: remove items from a vector using iterator


NM wrote:
Quote:
[..] 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


William
Guest
 
Posts: n/a
#6: Jan 4 '07

re: remove items from a vector using iterator


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

On Fri, 22 Dec 2006 04:08:19 +0800, NM <nittanymountain@gmail.comwrote:
Quote:
>
Victor Bazarov wrote:
Quote:
>happyvalley wrote:
Quote:
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<intIntVec;
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).
>>
Quote:
{
if(*intIterator == 2)
IntVec.erase(intIterator);
>>
>Replace those with
>>
> if (*intIterator == 2)
> intIterator = IntVec.erase(intIterator);
> else
> ++intIterator;
>>
Quote:
}
>>
>and get yourself a decent book on the Standard library.
>>
>V
>--
>Please remove capital 'A's when replying by e-mail
>I do not respond to top-posted replies, please don't ask
>
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
>
Bo Persson
Guest
 
Posts: n/a
#7: Jan 4 '07

re: remove items from a vector using iterator


William wrote:
Quote:
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

Quote:
>
On Fri, 22 Dec 2006 04:08:19 +0800, NM <nittanymountain@gmail.com>
wrote:
Quote:
>>
>Victor Bazarov wrote:
Quote:
>>happyvalley wrote:
>>>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<intIntVec;
>>>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
>>--
>>Please remove capital 'A's when replying by e-mail
>>I do not respond to top-posted replies, please don't ask
>>
>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

Closed Thread