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. 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
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 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
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.
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?
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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;
|
by: cylin |
last post by:
Dear all,
Here is my code.
------------------------------
#include <iostream>
#include <vector>
using namespace std;
class A {
public:
|
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;
|
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;
|
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:
...
|
by: markww |
last post by:
Hi,
I have a class like:
class CStorage {
map<int, vector<CSomething m_mData;
CSomething *m_pLastUsed;
};
|
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
|
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){...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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,...
|
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...
|
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...
| |