Valid pointer to map element after erase

G

gfiuni2

Hi,

Given the code below:

#include <iostream.h>
#include <string>
#include <map>

int main()
{
map<int, string > m;
string *ptr;

m[1]="one";
m[2]="two";

map<int, string >::iterator it=m.find(1);
ptr=&(it->second);

m.erase(m.find(2));

cout << *ptr << endl;
}



Is ptr a valid pointer after erasing elements from map?


Thanks in advance,
Jose Luis.
 
K

Kai-Uwe Bux

gfiuni2 said:
Hi,

Given the code below:

#include <iostream.h>

This header never was part of the standard. Upgrade to:

#include said:
#include <string>
#include <map>

int main()
{
map<int, string > m;
string *ptr;

m[1]="one";
m[2]="two";

map<int, string >::iterator it=m.find(1);
ptr=&(it->second);

m.erase(m.find(2));

cout << *ptr << endl;
}



Is ptr a valid pointer after erasing elements from map?

According to [23.1.2/8] erase (for associative containers) does not
invalidate references and iterators to elements other than the erased one.
It does not mention pointers, which appears to be an oversight. Pointers
are mentioned along with references and iterators where invalidation is
mentioned elsewhere (e.g., the standard says that swap for containers does
not invalidate pointers, reference, and iterators into a container). So
formally, your program may be undefined; but that appears to be a defect in
the standard and no implementation would actually invalidate the pointer
ptr in your code.


Best

Kai-Uwe Bux
 
A

amirkam1

gfiuni2 said:
Hi,

Given the code below:

#include <iostream.h>
#include <string>
#include <map>

int main()
{
map<int, string > m;
string *ptr;

m[1]="one";
m[2]="two";

map<int, string >::iterator it=m.find(1);
ptr=&(it->second);

m.erase(m.find(2));

cout << *ptr << endl;
}



Is ptr a valid pointer after erasing elements from map?


Thanks in advance,
Jose Luis.

No, it will be invalid.
The erase is going to destroy the string object stored in the map.

You can try this by adding a user defined object to the map and then
calling erase. The destructor will get called when erase is called.


amir kamerkar
 
K

Kai-Uwe Bux

gfiuni2 said:
Hi,

Given the code below:

#include <iostream.h>
#include <string>
#include <map>

int main()
{
map<int, string > m;
string *ptr;

m[1]="one";
m[2]="two";

map<int, string >::iterator it=m.find(1);
ptr=&(it->second);

m.erase(m.find(2));

cout << *ptr << endl;
}



Is ptr a valid pointer after erasing elements from map?


Thanks in advance,
Jose Luis.

No, it will be invalid.
The erase is going to destroy the string object stored in the map.

Did you notice that ptr points to the string whose key is 1 whereas the item
erased is the one whose key is 2?

You can try this by adding a user defined object to the map and then
calling erase. The destructor will get called when erase is called.


Best

Kai-Uwe Bux
 
A

amirkam1

gfiuni2 said:
Hi,

Given the code below:

#include <iostream.h>
#include <string>
#include <map>

int main()
{
map<int, string > m;
string *ptr;

m[1]="one";
m[2]="two";

map<int, string >::iterator it=m.find(1);
ptr=&(it->second);

m.erase(m.find(2));

cout << *ptr << endl;
}



Is ptr a valid pointer after erasing elements from map?


Thanks in advance,
Jose Luis.

No, it will be invalid.
The erase is going to destroy the string object stored in the map.

You can try this by adding a user defined object to the map and then
calling erase. The destructor will get called when erase is called.


amir kamerkar


Oops, I missed that the element being deleted and stored in pointer are
different.
 
R

red floyd

gfiuni2 said:
Hi,

Given the code below:

#include <iostream.h>
non standard header. Use #include said:
#include <string>
#include <map>

int main()
{
map<int, string > m;
string *ptr;

m[1]="one";
m[2]="two";

map<int, string >::iterator it=m.find(1);
ptr=&(it->second);

m.erase(m.find(2));

cout << *ptr << endl; std::cout << *ptr < std::endl;
}



Is ptr a valid pointer after erasing elements from map?

Yes. Pointers and iterators into an associative container are valid
after an erase (as long as you're not looking at the erased elements).

Side note about my earlier comments. You obviously are aware of the
Standard headers, as you're using <string> and <map>. Why are you still
using iostream.h?
 
J

Jose Luis

Side note about my earlier comments. You obviously are aware of the
Standard headers, as you're using <string> and <map>. Why are you still
using iostream.h?

I am working on this enviroment:

m3vmsa3.closedeb /tmp > aCC -V
aCC: HP ANSI C++ B3910B A.03.27
m3vmsa3.closedeb /tmp > uname -a
HP-UX m3vmsa3 B.11.00 U 9000/800 178951547 unlimited-user license



If I don't append ".h" to any input/output header the compiler
complains:

m3vmsa3.closedeb /tmp > aCC kk.c
Error 112: "kk.c", line 2 # Include file <iostream> not found.
#include <iostream>

Regards,
Jose Luis
 
M

Marcus Kwok

Jose Luis said:
I am working on this enviroment:

m3vmsa3.closedeb /tmp > aCC -V
aCC: HP ANSI C++ B3910B A.03.27
m3vmsa3.closedeb /tmp > uname -a
HP-UX m3vmsa3 B.11.00 U 9000/800 178951547 unlimited-user license


If I don't append ".h" to any input/output header the compiler
complains:

m3vmsa3.closedeb /tmp > aCC kk.c
Error 112: "kk.c", line 2 # Include file <iostream> not found.
#include <iostream>

<OT>
Try using the '-AA' parameter for aCC, which turns on
standards-compliant mode.
</OT>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top