std::map<std::string,std::string>::iterator it;
for (it = m_mapItems.begin(); it != m_mapItems.end(); it++)
{
// Do stuff here
// ...
}
🔒 Locked
Looping items in 'std::map' objects?
Advertisement
Original Post
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: 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.
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.
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.
Because you have no details, all I can do is guess what it is your problem is.
Here is my guess.
When you evalutate
In this case, a
To access the key for your element, call
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. =)
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. =)
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.
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.
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
Just remember with some implementations of std::map if you do the following:
It is going to add the key if it is not already there. So the best way to search the map is the following:
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!
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!
Quote:
Original post by bkt
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.
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
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
It really really looks like you're missing a header file, that's all.
--smw
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement