472,952 Members | 2,507 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

vector.erase(iterator iter) will change "iter" or not?

suppose I will delete an element pointed to by "iter".
like this:

vector<ints;
for(vector<int>::iterator iter=s.begin(); iter!=s.end(); iter++){
if(*iter==3)
s.erase(iter); //A
}

in line A, if element by "iter" is erased, will "iter" point to the
next element(now should be the current element) automatically?
Feb 21 '08 #1
13 7413
thomas wrote:
suppose I will delete an element pointed to by "iter".
like this:

vector<ints;
for(vector<int>::iterator iter=s.begin(); iter!=s.end(); iter++){
if(*iter==3)
s.erase(iter); //A
}

in line A, if element by "iter" is erased, will "iter" point to the
next element(now should be the current element) automatically?
The iterator that refers to the removed element and all elements
after the removed one are invalidated by that operation. IOW, the
Standard makes no attempt to define what the 'iter' would point to
after being erased.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 21 '08 #2
On Thu, 21 Feb 2008 15:16:11 +0100, thomas <Fr*********@gmail.comwrote:
suppose I will delete an element pointed to by "iter".
like this:

vector<ints;
for(vector<int>::iterator iter=s.begin(); iter!=s.end(); iter++){
if(*iter==3)
s.erase(iter); //A
}

in line A, if element by "iter" is erased, will "iter" point to the
next element(now should be the current element) automatically?
Do :

vector<ints;
for(vector<int>::iterator iter=s.begin(); iter!=s.end(); iter++){
if(*iter==3)
iter= s.erase(iter);
}
Feb 21 '08 #3
(And the higher-level answer to his question is that he should probably
be using std::remove anyway:

s.erase(remove(s.begin(), s.end(), 3), s.end());
What's this?
will "s.erase()" get all the elements after "3" removed?
Feb 21 '08 #4

"thomas" <Fr*********@gmail.coma écrit dans le message de news:
c6**********************************...oglegroups.com...
>
>(And the higher-level answer to his question is that he should probably
be using std::remove anyway:

s.erase(remove(s.begin(), s.end(), 3), s.end());

What's this?
will "s.erase()" get all the elements after "3" removed?
remove will remove all element equal to 3. beware, removing is not
erasing... Remove returns an iterator to the new end of the sequence s (it
does not actually modify s.end() ) , so you can use this iterator in the
vector::erase function. The size of the vector is not modified after a
remove, but the element between the returned end and the actual s.end() but
the element are unspecified.

so you can do

vector<int>::iterator NewEnd = remove(s.begin(), s.end(), 3);
s.erase(NewEnd, s.end);
Eric Pruneau
Feb 21 '08 #5

Just a small correction.
vector<ints;
for(vector<int>::iterator iter=s.begin(); iter!=s.end(); iter++){
if(*iter==3)
iter= s.erase(iter);
}
After the erase() call iter points at the next element in the
container. Thus when the iter++ is called you will effectively skip
an element or if the erased element is the last element it now points
one after end().

Also for efficiency it is a god idea to get in the habit of using
prefix increment (In this case it probably makes no difference but
for the generic case it does).

Feb 21 '08 #6
On Feb 22, 11:54 pm, Richard Herring <ju**@[127.0.0.1]wrote:
If it doesn't point anywhere, it points nowhere. "nowhere" looks pretty
invalid to me.
Non-sequitur ; there are three possible states for
pointers (or iterators): pointing to a valid object,
null, or indeterminate. By 'anywhere' I do not
include the indeterminate case.
Feb 22 '08 #7
On Feb 22, 8:47 pm, Triple-DES <fire13...@hotmail.comwrote:
That's an interesting point. Consider:
int * p = new int(441);
delete p;

Would you say that the last line changes the value of p?
Yes
Feb 22 '08 #8
In message
<ee**********************************@64g2000hsw.g ooglegroups.com>, Old
Wolf <ol*****@inspire.net.nzwrites
>On Feb 22, 11:54 pm, Richard Herring <ju**@[127.0.0.1]wrote:
[big snip noted]
>If it doesn't point anywhere, it points nowhere. "nowhere" looks pretty
invalid to me.

Non-sequitur ; there are three possible states for
pointers (or iterators): pointing to a valid object,
null, or indeterminate.
The standard refers to this state as "[having] singular values".
>By 'anywhere' I do not
include the indeterminate case.
But you obviously meant "not anywhere" to include singular states when
you wrote this:
>>previously it was well-defined and now it is indeterminate! It
doesn't point anywhere;
--
Richard Herring
Feb 22 '08 #9
On Feb 23, 2:38 am, Richard Herring <ju**@[127.0.0.1]wrote:
But you obviously meant "not anywhere" to include singular states when
you wrote this:
>previously it was well-defined and now it is indeterminate! It
doesn't point anywhere;
Actually I didn't. I'll try to use more precise
terms than "anywhere" in future.
Feb 22 '08 #10
On Feb 21, 6:45 pm, Martin York <Martin.YorkAma...@gmail.comwrote:

[...]
Also for efficiency it is a god idea to get in the habit of
using prefix increment (In this case it probably makes no
difference but for the generic case it does).
Measurements? Or is this just speculation on your part. (I've
actually measured with g++, and all of the standard iterators.
No measurable difference.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Feb 22 '08 #11
On Feb 23, 6:40 am, James Kanze <james.ka...@gmail.comwrote:
unsigned char before[ sizeof( std::vector<int>::iterator ] ;
memcpy( before, &iter, sizeof( std::vector<int>::iterator ) ) ;
v.erase( iter ) ;
memcmp( before, &iter, sizeof( std::vector<int>::iterator ) ) ;

the memcmp will return a value not equal to 0.
How does that work? I agree that it's legal, but I wouldn't
expect it anywhere except the DS9000; it seems that the
implementation, when faced with vector::erase, would have
to go out of its way to go and change bits in the original
'iter' that the parameter to vector::erase was copied from.
And trying to use the iterator will cause a core dump.
To be expected when using indeterminate 'values'.
Feb 23 '08 #12
In message
<a2**********************************@f47g2000hsd. googlegroups.com>,
James Kanze <ja*********@gmail.comwrites
>Old Wolf wrote:
>On Feb 23, 6:40 am, James Kanze <james.ka...@gmail.comwrote:
unsigned char before[ sizeof( std::vector<int>::iterator ] ;
memcpy( before, &iter, sizeof( std::vector<int>::iterator ) ) ;
v.erase( iter ) ;
memcmp( before, &iter, sizeof( std::vector<int>::iterator ) ) ;
the memcmp will return a value not equal to 0.
>How does that work?

About how you'd expect. The container knows about the iterators
which refer to it, and marks them as invalid whenever it
invalidates them.
>I agree that it's legal, but I wouldn't expect it anywhere
except the DS9000; it seems that the implementation, when
faced with vector::erase, would have to go out of its way to
go and change bits in the original 'iter' that the parameter
to vector::erase was copied from.

I'm not sure what you mean by "go out of its way".
Iterate through every copy of every iterator ever generated from that
container, to see if it references the region that's about to be
invalidated?
Every
pre-standard iterator I ever wrote did this. Logically, the
iterator knows about the container, and vice versa.
But physically it need not: see implementations where
vector<T>::iterator is implemented as plain T*.

--
Richard Herring
Feb 25 '08 #13
On Feb 26, 11:31 am, Richard Herring <ju**@[127.0.0.1]wrote:
In message
<79bc8204-9fcd-4cae-808b-d6b179de1...@v3g2000hsc.googlegroups.com>,
James Kanze <james.ka...@gmail.comwrites
But physically it need not: see implementations where
vector<T>::iterator is implemented as plain T*.
It's not *required* by the STL. All of the good implementations
do it.
No doubt, if you define "good" implementations as the ones
that do it.
I define good by those which are standards conformant, and work
as advertised. Basically, Dinkumware and g++.
It's a shame they can't do the same for all the references and
pointers that also become invalidated.
That is, of course, a language issue, rather than a library
issue. The Boehm collector goes a long way in this regard, but
obviously, it can't help in cases where the memory is managed
elsewhere (e.g. in std::vector, but also things like pointers to
local variables).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Feb 26 '08 #14

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

Similar topics

41
by: AngleWyrm | last post by:
I have created a new container class, called the hat. It provides random selection of user objects, both with and without replacement, with non-uniform probabilities. Uniform probabilities are a...
13
by: Mike Austin | last post by:
Hi all. Just working on a small virtual machine, and thought about using vector iterators instead of pointer arithmetic. Question is, why does an iterator plus any number out of range not...
2
by: Michael Olea | last post by:
Hiya. Frequently (as in at least twice now) I find that when I write some sort of container I want at least two out of three possible versions: o fixed max size known at compile time o fixed...
5
by: Maett | last post by:
Hi. When I try to compile this piece of Code with VC.NET 2003 // code start #include <vector> template< class T > struct Marker { public:
4
by: Generic Usenet Account | last post by:
I am seeing some unexpected behavior while using the STL "includes" algorithm. I am not sure if this is a bug with the template header files in our STL distribution, or if this is how it should...
0
by: rokuingh | last post by:
ok, so i've been working on this one for quite a while, and the code is very big so i'm just going to give the relevant parts. this is a program that builds polymers (chemical structures of repeated...
4
by: arnuld | last post by:
i wrote a programme to create a vector of 5 elements (0 to 4), here is the code & output: #include <iostream> #include <vector> int main() { std::vector<intivec; // dynamically create a...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.