473,770 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<intIntVe c;
vector<int>::it erator intIterator;

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

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

Dec 21 '06 #1
6 3405
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<intIntVe c;
vector<int>::it erator intIterator;

for(int i=0; i<10;i++) IntVec.push_bac k(i);
for(int i=0; i<10;i++) IntVec.push_bac k(i);
for(int i=0; i<10;i++) IntVec.push_bac k(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(in tIterator);
Replace those with

if (*intIterator == 2)
intIterator = IntVec.erase(in tIterator);
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<intIntVe c;
vector<int>::it erator intIterator;

for(int i=0; i<10;i++) IntVec.push_bac k(i);
for(int i=0; i<10;i++) IntVec.push_bac k(i);
for(int i=0; i<10;i++) IntVec.push_bac k(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(in tIterator);

Replace those with

if (*intIterator == 2)
intIterator = IntVec.erase(in tIterator);
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.comwrot e:
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.comwrot e:
>
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<intIntVe c;
vector<int>::it erator intIterator;

for(int i=0; i<10;i++) IntVec.push_bac k(i);
for(int i=0; i<10;i++) IntVec.push_bac k(i);
for(int i=0; i<10;i++) IntVec.push_bac k(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(in tIterator);

Replace those with

if (*intIterator == 2)
intIterator = IntVec.erase(in tIterator);
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(in tIterator);

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<intIn tVec;
vector<int>: :iterator intIterator;

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

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

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

{
if(*intItera tor == 2)
IntVec.erase (intIterator);

Replace those with

if (*intIterator == 2)
intIterator = IntVec.erase(in tIterator);
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
12713
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 the difference between std::remove and std::erase? Thanks for your time!
14
5027
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 points in space that arrive randomly into groupings based on (x, y, z) dimensions, so I use std::map to do that. Once the sorting is done I need to find the interactions between groups, so I've been using iterators to step through the map. As a...
5
2566
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 middle of the vector. I am not using iterators at the moment, but if i had i think that i could have used "erase". This is my code now:
18
2877
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 contains another std::vector<CPosition>. Because one of the players is the client itself (and the size of the vector<CPlayer> doesn't change during a game), i thought i could store a std::vector<CPlayer>::iterator "localplayer" that points to the...
11
2746
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
4956
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 new, logical end pointer, so that the following myList::iterator end = myListObj.remove(myInt); myListObj.erase(end, myListObj.end()); is possible and removes the "invalid" items at the end of the list.
7
9978
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 algorithm? vector<int> srcVector; vector<int> destVector; for (vector<int>::iterator itr = srcVector; itr != srcVector; itr++) { int i = (*itr);
3
1690
by: Wing | last post by:
Hello, I have the following code: //////////////////////////// class Obj { public: int time;
26
13807
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 and return "12384" What is the most efficient, fastest way to approach this? Thanks,
0
9453
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10254
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10099
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8929
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3607
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.