473,385 Members | 1,353 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

remove items from a vector using iterator

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);
}

Dec 21 '06 #1
6 3384
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
Dec 21 '06 #2
NM

Victor Bazarov wrote:
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

Dec 21 '06 #3
"NM" <ni*************@gmail.comwrote:
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.
Dec 21 '06 #4
NM wrote:
[..] 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
Dec 21 '06 #5
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 <ni*************@gmail.comwrote:
>
Victor Bazarov wrote:
>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
Jan 4 '07 #6
William wrote:
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

>
On Fri, 22 Dec 2006 04:08:19 +0800, NM <ni*************@gmail.com>
wrote:
>>
Victor Bazarov wrote:
>>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

Jan 4 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Christopher Armstrong | last post by:
Hello! I'm trying to write a part of a program that will remove all files in its directory. I have tried the std::remove feature of the standard library, but I don't know its syntax. Also, what's...
14
by: Jim West | last post by:
I'm curious why I might be getting such a large performance difference between using a map iterator and a vector iterator. This is a computational electromagnetics code where I have to separate...
5
by: Yngve | last post by:
Hi! I have a (newbie) problem wich i would become glad if someone could help me with. I have a Vector of pointers to instances of another class. I would like to remove a position in the...
18
by: Janina Kramer | last post by:
hi ng, i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPlayer> to store informatik about the players. CPlayer is a class that...
11
by: koperenkogel | last post by:
Dear cpp-ians, I am working with a vector of structures. vector <meta_segment> meta_segm (2421500); and the structure look like: struct meta_segment { float id; float num;
6
by: Arne Claus | last post by:
Hi If've just read, that remove() on a list does not actually remove the elements, but places them at the end of the list (according to TC++STL by Josuttis). It also says, that remove returns a...
7
by: Allerdyce.John | last post by:
How can I emove an item in STL iterator without use the STL algorithm? I know we can use stl erase, find_if to remove items from a STL vector , but how can I do the same without using STL...
3
by: Wing | last post by:
Hello, I have the following code: //////////////////////////// class Obj { public: int time;
26
by: Brad | last post by:
I'm writing a function to remove certain characters from strings. For example, I often get strings with commas... they look like this: "12,384" I'd like to take that string, remove the comma...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.