Jump to content
  • Advertisement
Sign in to follow this  
TFS_Waldo

Looping items in 'std::map' objects?

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
have you included header ? because your itreation loop looks absolutely right

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by paic
As 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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!