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);
} 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
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
"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.
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
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
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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!
|
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...
|
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:
|
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...
|
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;
| |
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.
|
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);
|
by: Wing |
last post by:
Hello,
I have the following code:
////////////////////////////
class Obj
{
public:
int time;
|
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,
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |