28

I have a std::map mymap

Now, if I insert values in the map like:

std::map <string, string> mymap;
mymap["first"] = "hi";
mymap["third"] = "how r you";
mymap["second"] = "hello";

Now I want to iterate over the map and print the value in sorted(keys) manner:

map<string, string>::iterator itr;
for(itr = mymap.begin(); itr != mymap.end(); itr++)
{
   string newline = itr->second;
   cout << newline << endl;
}

Output should be:

hi 
hello 
how r you 

I thought that by default map stores in sorted keys manner but I'm getting the same order in output as I'm giving in input. Do I need to provide my sort function for this or need to do something extra before iterating over the map?

3
  • 3
    The for loop refers to file_line and not mymap. I take it this is not the actual code as first is not quoted in population of mymap. Commented Dec 13, 2011 at 13:58
  • I am not quite sure about the C++ std::map implementation, but such hashtables are not usually sorted. They are meant to be accessed via indexer, not traversed. Commented Dec 13, 2011 at 14:00
  • 2
    @user983064 std::map is a binary tree, ordered by key. C++11 has hash tables, such as std::unordered_map. Commented Dec 13, 2011 at 14:49

4 Answers 4

47

The elements in std::map are ordered (by default) by operator< applied to the key.

The code you posted, with minor edits, worked for me as you expected:

std::map <string, string> mymap;
mymap["first"]="hi";
mymap["third"]="how r you";
mymap["second"]="hello";

for (std::map<string, string>::iterator i = mymap.begin(); i != mymap.end(); i++)
{
    cout << i->second << "\n";
}

Prints:

hi
hello
how r you
Sign up to request clarification or add additional context in comments.

6 Comments

uhmmmm ... what about "fourth"?? Should it be after "first" or "third"? May be he needs to customize the comparison predicate...
@EmilioGaravaglia, yes he would or just use an int as the key.
auto? this question wasn't tagged as c++11
@julio.alegria, there you go.
Just a follow-up question on this response, what is the difference between for (std::map<string, string>::iterator i = mymap.begin(); i != mymap.end(); i++) and for (std::map<string, string>::iterator i = mymap.begin(); i != mymap.end(); ++i) where the iterator is incremented before rather than after?
|
9

The map is actually a tree, and is sorted by KEY order. You are printing itr->second which is the VALUE not the KEY. If you want your key/value pairs sorted by VALUE, use the VALUE as key instead, or store everything in another container (say an array), then sort them.

1 Comment

What you said is correct, so KEYS are: "first" < "second" < "third", thus, we should expect the same result as the OP said: hi | hello | how r you, but he said: I'm getting the same order in output as I'm giving in input, that is: hi | how r you | hello.
4

std::map is already ordered. If you were using unordered_map, now you'd have a problem!

Entries in std::map are ordered by the key, or itr->first. itr->second as you have it, refers to the value associated with the key.

Further more, you're not iterating over the map, you're iterating over file_line (I don't know what that is, but I'm going to assume it's different from mymap. That is what you should be iterating over).

Comments

2

The standard defines:

The fundamental property of iterators of associative containers is that they iterate through the containers in the non-descending order of keys where non-descending is defined by the comparison that was used to construct them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.