C++ 在 std::map 中排序,其中键是 std::string
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8490359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sorting in std::map where key is a std::string
提问by Ruchi
I have a std::map mymap
我有一个 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:
现在我想遍历地图并以 sorted(keys) 方式打印值:
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?
我认为默认情况下地图以排序键的方式存储,但我在输出中获得的顺序与我在输入中给出的顺序相同。我需要为此提供我的排序功能还是需要在迭代地图之前做一些额外的事情?
回答by hmjd
The elements in std::map
are ordered (by default) by operator<
applied to the key.
中的元素std::map
按operator<
应用于键进行排序(默认情况下)。
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
回答by Giovanni Funchal
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.
该map
实际上是一个树,并得到了主要的顺序进行排序。您正在打印的itr->second
是 VALUE 而不是 KEY。如果您希望按 VALUE 对键/值对进行排序,请改用 VALUE 作为键,或者将所有内容存储在另一个容器(例如数组)中,然后对它们进行排序。
回答by MGZero
std::map is already ordered. If you were using unordered_map, now you'd have a problem!
std::map 已被订购。如果您使用的是 unordered_map,现在您会遇到问题!
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.
std::map 中的条目按键或 itr->first 排序。itr->second 是指与键关联的值。
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).
此外,您不是在地图上迭代,而是在 file_line 上迭代(我不知道那是什么,但我将假设它与 mymap 不同。这就是您应该迭代的内容)。
回答by Shawnone
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.
关联容器的迭代器的基本属性是它们以键的非降序顺序遍历容器,其中非降序由用于构造它们的比较定义。