473,395 Members | 1,458 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,395 software developers and data experts.

How to use map::iterator

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
Jul 22 '05 #1
5 42873

"music4" <mu****@163.net> wrote in message
news:c6********@netnews.proxy.lucent.com...
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
Jul 22 '05 #2

"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:c6************@ID-221354.news.uni-berlin.de...

"music4" <mu****@163.net> wrote in message
news:c6********@netnews.proxy.lucent.com...
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
Jul 22 '05 #3
music4 wrote:
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";
}
Jul 22 '05 #4
"music4" <mu****@163.net> wrote in message news:<c6********@netnews.proxy.lucent.com>...
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
Jul 22 '05 #5
"music4" <mu****@163.net> wrote in message news:<c6********@netnews.proxy.lucent.com>...
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;
}
Jul 22 '05 #6

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

Similar topics

0
by: sks_cpp | last post by:
I am trying to wrap the map iterator for keys and values. However, I seem to run into problems with the values for const_iterator - it works for all other combinations. Below I list my code and...
2
by: Bobo | last post by:
Hello, all. I have just wrote the following little class, and I'd like you to give me your opinion about it. I compiles with VC6 but I'm not sure if it is totally right. I'm trying to explain...
4
by: John | last post by:
I was wondering why the STL RB Tree implementation is used when the iterators of map are incremented/decremented to yield successors and predecessors? This takes O(log n) time (and a lot of memory...
6
by: John | last post by:
I want to store an array of pointers (void *) to iterators inside a map. The idea is to reach the map <key,info> pair faster if the data is in the array. If not use a O(log) search. But stl...
5
by: Bill Oliver | last post by:
Help! I am writing an image processing package. For one constructor, I allow creating an image from a map of points and color values. The points are of a "position" class and the colors are...
2
by: Rakesh Kumar | last post by:
I am encountering the following issue with STL map iterator - wrapped within a template. ( I am wrapping it withing a template since I want to hide the map implementation from the user . At a later...
16
by: xyz | last post by:
I have to run the simulation of a trace file around (7gb contains 116million entries)... presently i am using vector iterators to check the conditions in my program..... it is taking 2 days to...
15
by: puzzlecracker | last post by:
I see that a lot of former in the code, and wonder if there is a technical reason for that
5
by: Micheal Smith | last post by:
I'm looking for a good method to convert a map::iterator to a std::string. I've tried googling for a while, but haven't come up with much. Perhaps I'm kludging things here. Either way any advice...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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: 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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
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.