# Looping items in 'std::map' objects?

This topic is 4342 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Well, I've been working on a simple project of mine, that I started yesterday. I have a class that uses 'std::map' to store some items, but I can't find out how to loop through the map. The map is 'std::map<std::string, std::string> m_mapItems;'. It's that simple. I thought that the standards were to use iterators, but I can't figure out how. I've done it with an 'std::vector' object. I just can't figure out how to iterate through a 'std::map'. Can someone help me out, please? =) I tried something like this:
std::map<std::string,std::string>::iterator it;

for (it = m_mapItems.begin(); it != m_mapItems.end(); it++)
{
// Do stuff here
// ...
}


But it gives me a load of errors. I can't remember what right now, because I'm on a diff. machine. Thanks in advance, Matt U.

##### Share on other sites
Looks fine. But you might consider std::for_each

##### Share on other sites
have you included header ? because your itreation loop looks absolutely right

##### Share on other sites
The code should work fine.
When you can compile, post your errors, it will help a lot to find the source of the problem.

Just a quick note, though : the std::map requires its key to have a less operator implemented. Such an operator is not implemented by the default std::string, and as such you can't use std::map<std::string, [put_another_type_here]> as is. You have to first create the less operator for std::string.

Check if you still have the errors with std::map<int, int> or std::map<int, std::string>. If it compile, then you simply have to create a global less operator which takes 2 std::string's as parameters.

##### Share on other sites
Well, it's in VS .NET 2003 Pro. And I get A LOT of compiler errors. They are complaining about me using '!=' and things like that.

##### Share on other sites
Because you have no details, all I can do is guess what it is your problem is.

Here is my guess.

When you evalutate
*it
, you get a
std::pair<key, value>&
.

In this case, a
std::pair<std::string,std::string>&
. (in some STLs, you get a
std::pair<const std::string,std::string>&
.)

To access the key for your element, call
it->first
. To access the value, call
it->second
.

In general, to solve a problem of this nature, write a minimal chunk of code in a minimal source file with minimal #includes that has your problem in it. Often this will let you figure out what is going wrong. If that fails, you now have a very concise file that demonstrates your problem to other people. =)

##### Share on other sites
The != operator ? I thought std::map used < operator :/
Well, anyway. The std::map organise its data by sorting by its key (the first value of a pair) So if you use a key type that doesn't have any comparison operator, the std::map can't sort, and there is a lot of errors.

As I said, your problem probably comes from the fact you're using the std::string which doesn't define <, !=, etc. operators.

##### Share on other sites
Oh yeah, I almost forgot. It points some of the errors to "/vc7/include/xtree". I don't know for sure what. I'll go compile again, and I'll be back.

##### Share on other sites
Quote:
 Original post by paicAs I said, your problem probably comes from the fact you're using the std::string which doesn't define <, !=, etc. operators.

I don't know what messed up standard library implementation you're using. The SC++L std::string class implements all of the standard relational operators.

Σnigma

##### Share on other sites
Just remember with some implementations of std::map if you do the following:
if( keyMap["TheKey"] )

It is going to add the key if it is not already there. So the best way to search the map is the following:
keyMap::iterator it = keyMap.find( ... );if( it != keyMap.end() )

I've ran into that problem a few times. You might want to invest in setting up STLport so that you can have a uniform version of STL running depending on your compiler (or if you're sticking with one, that really shouldn't matter). Good luck!

##### Share on other sites
Quote:
 Original post by bktJust remember with some implementations of std::map if you do the following:if( keyMap["TheKey"] )It is going to add the key if it is not already there.

Again, there is no "some" about it. Under any standards conforming map implementation the expression map[key] will insert a default-constructed element into the map with the key key if no such key already exists.

Σnigma

##### Share on other sites
I'd go with NotAYakk's suggestion as the most likely problem. That issue confused me the first time I used std::map, and I've seen it throw other people off before too. Very easy to do.

##### Share on other sites
Do ignore most of the above posts. std::string does have comparison operators, addressing elements of maps is not relevant to the OP's question, and all conforming C++ standard library implementations are conforming (this includes Microsoft's since about 6 years after the standard was published in November 1997, and GCC since version 3.0), so don;t blame your vendor.

It really really looks like you're missing a header file, that's all.

--smw