How to use map::iterator

M

music4

Greetings,

I want to STL map class. But I don't know how to use iterator. Let me state
my question by following small sample:

map<int, int> mymap;

// insert some <key,value> in mymap

iterator p = mymap::begin();

// QUESTION HERE: how to get key and value from p?

Thanks in advance!
Evan
 
S

Sharad Kala

music4 said:
Greetings,

I want to STL map class. But I don't know how to use iterator. Let me state
my question by following small sample:

map<int, int> mymap;

// insert some <key,value> in mymap

iterator p = mymap::begin();

Something like -
map<int, int> mymap;
mymap[0]=10;
mymap[1]=20;
map<int, int>::const_iterator itr;

for(itr = mymap.begin(); itr != mymap.end(); ++itr){
cout << "Key: " << (*itr).first << " Value: " << (*itr).second;
}

-Sharad
 
J

John Harrison

Sharad Kala said:
music4 said:
Greetings,

I want to STL map class. But I don't know how to use iterator. Let me state
my question by following small sample:

map<int, int> mymap;

// insert some <key,value> in mymap

iterator p = mymap::begin();

Something like -
map<int, int> mymap;
mymap[0]=10;
mymap[1]=20;
map<int, int>::const_iterator itr;

for(itr = mymap.begin(); itr != mymap.end(); ++itr){
cout << "Key: " << (*itr).first << " Value: " << (*itr).second;

itr->first and itr->second is a little easier.

john
 
J

Jeff Schwab

music4 said:
Greetings,

I want to STL map class. But I don't know how to use iterator. Let me state
my question by following small sample:

map<int, int> mymap;

// insert some <key,value> in mymap

iterator p = mymap::begin();

/* ITYM
*/
mymap::iterator p = mymap.begin( );
// QUESTION HERE: how to get key and value from p?

#include <map>
#include <iostream>

int main ( )
{
typedef std::map< int, int > Map;

Map map;

map[ 0 ] = 1;
map[ 2 ] = 3;

Map::iterator p = map.begin( );

std::cout << "map[ " << p->first
<< " ] = " << p->second << ";\n";
}
 
D

Dave Moore

music4 said:
Greetings,

I want to STL map class. But I don't know how to use iterator. Let me state
my question by following small sample:

map<int, int> mymap;

// insert some <key,value> in mymap

iterator p = mymap::begin();

// QUESTION HERE: how to get key and value from p?

Thanks in advance!
Evan

Well, first of all, whenever you have this sort of question you should
look up the declaration of std::map in your favorite reference:
(e.g. http://www.sgi.com/tech/stl), or even just read the header file
for your local implementation .. that should get you on the right
track.

That said, this is one of the places where the C++ STL can be a bit
confusing ... the confusion arises from the fact that:

std::map<T,U>::iterator::value_type==std::pair<T,U>

where std::pair stores the "key" (T) and "value" (U) elements as first
and second, respectively. Thus for your declaraion above, p->first
will give you the key and p->second will give you the value.

One other aspect of std::map that can be a bit confusing is that the
subscripting operator[] returns a reference to the relevant value (U),
but the find method returns an iterator. Then to make things even
more confusing, the insert method returns std::pair<iterator, bool>,
where the bool informs you of whether the insert was successful or
not. This leads to syntax like:

// for OP's declaration of mymap above
std::pair<std::map<int,int>::iterator, bool> test;
test=mymap.insert(std::pair<int,int>(101,749));
if (test.second) {
// insert succeeded -- assign new value to inserted element
(test.first)->second = 149; // !!!!
}

This can be cleaned up a bit using appropriate typedefs, but it still
takes a while to get used to the awkward syntax.

HTH, Dave Moore
 
J

Jerry Coffin

music4 said:
Greetings,

I want to STL map class. But I don't know how to use iterator. Let me state
my question by following small sample:

Most of the time, iterators should be used as parameters to
algorithms. Something that uses an iterator directly should normally
be packaged up as an algorithm.

Regardless of that, however, in the case of map what you have acts
like a pointer to an std::pair<key, data>, so you'd use it something
like this:

#include <map>
#include <string>
#include <iterator>
#include <iostream>

int main() {
std::map<std::string, int> things;

things["this"] = 1;
things["that"] = 2;

std::map<std::string, int>::iterator p = things.begin();

std::cout << p->first << ": " << p->second;
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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top