Hi, all
I wirte a small program to test some properties of map and vector. It
seems that the pointers I got can only support add opertion, and
cannot support substract operation. Is there anything wrong with my
program?
Thanks a lot!
#include <map>
#include <vector>
#include <iostream>
using namespace std;
typedef vector<int> IntVector;
typedef vector<int>::iterator IntVectorIterator;
typedef map<int, IntVectorIterator> IntMap;
typedef IntMap::iterator IntMapIterator;
int main()
{
IntVector ivec;
IntVectorIterator iit;
IntMap imap;
ivec.clear();
imap.clear();
iit = ivec.insert(ivec.begin(), 5 );
imap.insert( make_pair(5, iit) );
iit = ivec.insert(ivec.begin(), 19 );
imap.insert( make_pair(19, iit) );
iit = ivec.insert(ivec.begin(),22 );
imap.insert( make_pair(22, iit) );
IntMapIterator imit= imap.find(5);
cout<<"ivec.size() = "<<ivec.size()<<endl;
cout<< *(imit->second)<<endl;;
cout<< "&(*(imit->second))="<< &(*(imit->second))<<endl;
// expect 19, but got zero
cout<< "*(&(*(imit->second))-1)="<< *( &(*(imit->second)) - 1
)<<endl;
//expect 22, but get zero
cout<< "*(&(*(imit->second))-2)="<< *( &(*(imit->second)) - 2
)<<endl;
return 0;
} 1 1262 yu******@gmail.com wrote: I wirte a small program to test some properties of map and vector. It seems that the pointers I got can only support add opertion, and cannot support substract operation. Is there anything wrong with my program?
Yes. You seem to think that iterators remain OK all the time. That is
simply not true. std::vector is one of the most unreliable containers
in that respect. Its iterators get invalidated (become invalid) the most
of all. There are particular rules about that, and you can read about
them in books, but you can safely assume that any operation of inserting
or removing an element in/from a vector invalidates _all_ iterators.
There is no work-around, essentially. You shouldn't hold onto iterators
to elements of a vector. Thanks a lot!
#include <map> #include <vector> #include <iostream>
using namespace std;
typedef vector<int> IntVector; typedef vector<int>::iterator IntVectorIterator; typedef map<int, IntVectorIterator> IntMap; typedef IntMap::iterator IntMapIterator;
int main() { IntVector ivec; IntVectorIterator iit; IntMap imap;
ivec.clear(); imap.clear();
iit = ivec.insert(ivec.begin(), 5 ); imap.insert( make_pair(5, iit) );
iit = ivec.insert(ivec.begin(), 19 );
Right here the iterator stored in your 'imap' with the key 5 has already
been invalidated.
imap.insert( make_pair(19, iit) );
iit = ivec.insert(ivec.begin(),22 ); imap.insert( make_pair(22, iit) );
Now both iterators with keys 5 and 19 are invalid. IntMapIterator imit= imap.find(5);
I am sure it will find the right element here. The only caveat is that
the second part of that element is not valid any longer.
cout<<"ivec.size() = "<<ivec.size()<<endl; cout<< *(imit->second)<<endl;; cout<< "&(*(imit->second))="<< &(*(imit->second))<<endl;
Boom!!! Undefined behaviour. You attempted to use the 'imit->second'
by dereferencing it (invoking operator* for it), which for an invalid
iterator is undefined. // expect 19, but got zero cout<< "*(&(*(imit->second))-1)="<< *( &(*(imit->second)) - 1 )<<endl;
//expect 22, but get zero cout<< "*(&(*(imit->second))-2)="<< *( &(*(imit->second)) - 2 )<<endl;
return 0; }
HTH
V This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Henry Jordon |
last post by:
hello I was wondering if someone could help me get a main going on
this project I've completed the header file that the professor started
us on but not really sure how to get the main going. If...
|
by: ypjofficial |
last post by:
Hello All,
While readling the C and C++ literature i came to know that the OS
allocates separate protected memory space for each running program and
the program do not interfare into one another's...
|
by: Andrew |
last post by:
Hi all , i am supposed to create a program that performs a specific
operation to a 2-D matrix whose elements are positive (or zero ).
This operation is repeated T times on the matrix and only the...
|
by: tigrfire |
last post by:
I've been working on a program to try and play a game of Craps, based
on a version I found elsewhere - I didn't code the original, but I
added a few things such as a balance and wager system. I'm...
|
by: cj |
last post by:
Thanks to everyone that has helped me. Now I'm trying to write my first
program. I have an example of one that I need to write about. Any help
getting me started is appreciated. I'm having trouble...
|
by: growse |
last post by:
Right, I've got a 2 c# programs here. Lets call them A and B. My aim is
to send a simple string from B to A.
A is always running. I've overridden the WndProc method to give me
messages that are...
|
by: dspfun |
last post by:
I would like to analyze my running c-program.
What I would like to know for example is the range of the entire
address space of my running c-program (memory reserved for/by the
running program),...
|
by: pereges |
last post by:
I have a C program which I created on Windows machine. I have compiled
and executed the program on windows machine and it gives me the
consistent output every time i run it. for eg.
input a = 2,...
|
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: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |