473,396 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Valid pointer to map element after erase


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.

Aug 24 '06 #1
7 2507
gfiuni2 wrote:
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

Aug 24 '06 #2
gfiuni2 wrote:
>
Hi,

Given the code below:

#include <iostream.h>
This header never was part of the standard. Upgrade to:

#include <iostream>
#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
Aug 24 '06 #3
am******@yahoo.com wrote:
gfiuni2 wrote:
>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

Aug 24 '06 #4

amirk...@yahoo.com wrote:
gfiuni2 wrote:
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.

Aug 24 '06 #5
gfiuni2 wrote:
Hi,

Given the code below:

#include <iostream.h>
non standard header. Use #include <iostream>
#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 <stringand <map>. Why are you still
using iostream.h?

Aug 24 '06 #6
Side note about my earlier comments. You obviously are aware of the
Standard headers, as you're using <stringand <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 <iostreamnot found.
#include <iostream>

Regards,
Jose Luis

Aug 25 '06 #7
Jose Luis <jo*****************@gmail.comwrote:
>
>Side note about my earlier comments. You obviously are aware of the
Standard headers, as you're using <stringand <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 <iostreamnot found.
#include <iostream>
<OT>
Try using the '-AA' parameter for aCC, which turns on
standards-compliant mode.
</OT>

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Aug 25 '06 #8

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

Similar topics

1
by: kaede | last post by:
Hi all, I recently came across the following code: // some data struct Data { // ... some data }; // a list to hold the data vector<Data> dataList;
1
by: cylin | last post by:
Dear all, Here is my code. ------------------------------ #include <iostream> #include <vector> using namespace std; class A { public:
10
by: Bob | last post by:
Here's what I have: void miniVector<T>::insertOrder(miniVector<T>& v,const T& item) { int i, j; T target; vSize += 1; T newVector; newVector=new T;
24
by: Arin Chaudhuri | last post by:
Hi, Is the code below that gives the bytes that make up a double valid? /******/ #include <stdio.h> int main(void) { double w=0.1; unsigned char *p;
28
by: Wonder | last post by:
Hello, I'm confused by the pointer definition such as int *(p); It seems if the parenthesis close p, it defines only 3 integers. The star is just useless. It can be showed by my program: ...
7
by: markww | last post by:
Hi, I have a class like: class CStorage { map<int, vector<CSomething m_mData; CSomething *m_pLastUsed; };
7
by: JH Programmer | last post by:
Hi, is there any ways that allow us to delete an element in between? say int_val: 1 int_val: 2 int_val: 3
5
by: JoeC | last post by:
I am trying to make an iterator work with pointers. I wrote this code and it works but with an upgrade to my program vector is now unit* instead of unit type. void kill(std::vector<unit>& u){...
2
by: desktop | last post by:
Are there any case where iterators in a std::list gets invalidated besides from the iterator pointing to an element thats deleted? It seems that its only the std::vector that invalidates...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...
0
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...

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.