|
|
1
26
地图存储为按关键字顺序排序的树。你想要10个最小(或最大)的整数值,以及它们的键,对吗?
在这种情况下,迭代映射并将所有键值对放入一个向量对中(
|
|
|
2
7
boost::multi_index 。它看起来如下:
|
|
|
3
1
这可能不是最优雅的方式,但您可以通过集合中的值对它们进行排序,如下所示: #include <map>
#include <set>
#include <iostream>
#include <string>
using namespace std;
struct sortPairSecond
{
bool operator()(const pair<string, int> &lhs, const pair<string, int> &rhs)
{
return lhs.second < rhs.second;
}
};
int main (int argc, char *argv[])
{
cout << "Started...\n";
map<string, int> myMap;
myMap["One"] = 1;
myMap["Ten"] = 10;
myMap["Five"] = 5;
myMap["Zero"] = 0;
myMap["Eight"] = 8;
cout << "Map Order:\n---------------\n";
set<pair<string,int>, sortPairSecond > mySet;
for(map<string, int>::const_iterator it = myMap.begin(); it != myMap.end(); ++it)
{
cout << it->first << " = " << it->second << "\n";
mySet.insert(*it);
}
cout << "\nSet Order:\n--------------\n";
for(set<pair<string, int> >::const_iterator it = mySet.begin(); it != mySet.end(); ++it)
{
cout << it->first << " = " << it->second << "\n";
}
return 1;
}
|
|
|
4
1
|
|
|
5
1
以下是我工具箱里为这种场合准备的东西:
|
|
|
Julia · 矢量中相加为总和S的值的数量 3 年前 |
|
|
C_Rod · 在模板方法中确定STL容器中项目的数据类型 4 年前 |
|
|
quantumwell · 将空向量放入std::map() 8 年前 |
|
|
OutOfBound · 对未初始化内存使用算法的优点 8 年前 |
|
|
DarthRubik · 在使用列表删除之后,迭代器如何不无效 8 年前 |