Why this program give out zero ?

A

Andy Yang

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;
}
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top