Re: <map> erase

Tech-Archive recommends: Fix windows errors by optimizing your registry



> Hmm, wait, I don't understand how that is proper. In ++x, the iterator is
> incremented before being passed to erase. In x++, the iterator is
> incremented after being passed to erase...

Nope. The old value of x is what is passed to map erase.
But x is incremented to the next iterator position _before_ the call to
erase().
It is about what side effects are completed when a "sequence point" occurs I
believe -- from C.
After erase() completes, the old value of x is no longer a valid iterator.
But that does not matter as x is now at the next position -- and still
valid.
Post-increment is exactly what is wanted.

>..., but at that point it is invalidated. Even if you were to make a copy
>of x then increment it after erase it would not be valid.
>
> So then I am confused, does that mean that x++ copies x to t, then
> increments x, then returns t? That's the only way I can see how the below
> would work.

That is exactly what it does. If you have the snippet

extern int func(int, int, int);

int x = 1;
int y = 2;
int z = 3;

func(x++, y++, z++);

then for C & C++, the compilers are supposed to pass the current values of
x, y, z to func() but before func() is called, the increments are supposed
to have happened. So a compiler is free to implement it is

// A
int xold = x;
int yold = y;
int zold = z;
x += 1;
y += 1;
z += 1;
// B
func(xold, yold, zold);

and the statements between A & B can occur in any order as long as recording
of old value occurs before appropriate increment

But this is an invalid translation:

// A
int xold = x;
int yold = y;
int zold = z;
// B
func(xold, yold, zold);
x += 1;
y += 1;
z += 1;

The increments are not supposed to occur after the function call.

Stephen Howe


.



Relevant Pages

  • Re: Question about void pointers
    ... The ++ operator increments its operand by 1, ... next adjacent long int object in memory, ... because of the way pointer subtraction is defined. ... Yet another instance of undefined behavior that happens to "work" on ...
    (comp.lang.c)
  • Re: graphics in c
    ... The first repetition of the loop calls putpixel, ... increments x to 11, y to 10.7 and i to 2. ... void slopelessthanone(int x1, int x2, int y1, double m) ... Your other problem (and this might explain your reported problem of ...
    (comp.lang.c)
  • Re: graphics in c
    ... increments x to 11, y to 10.7 and i to 2. ... void slopelessthanone(int x1, int x2, int y1, double m) ... there is something wrong with putpixel() ... Your other problem (and this might explain your reported problem of ...
    (comp.lang.c)
  • Re: Erasin Vector Element
    ... > erase a certain vector element based on what number the user selects. ... > Compiler: Default compiler ... a reference to int, or a reference to const int, or an int. ...
    (comp.lang.cpp)
  • Re: suprising output !!
    ... int main{ ... increments can be written as ++ rather than += 1. ... For convenience also, the pre-increment is applied first and the postincrement last. ... This is also to help out the compiler writers a bit - it's the sort of thing that might be hard to catch in parser. ...
    (comp.lang.c)