C++ 从 unordered_map 获取键和值列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8483985/
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
Obtaining list of keys and values from unordered_map
提问by Faheem Mitha
What is the most efficient way of obtaining lists (as a vector
) of the keys and values from an unordered_map
?
vector
从 an获取键和值的列表(作为 a )的最有效方法是unordered_map
什么?
For concreteness, suppose the map in question is a unordered_map<string, double>
.
I'd then like to obtain the keys as a vector<string>
, and the values as a vector<double>
.
为具体起见,假设所讨论的地图是 a unordered_map<string, double>
。然后我想将键作为 a vector<string>
,将值作为 a vector<double>
。
unordered_map<string, double> um;
vector<string> vs = um.enum_keys();
vector<double> vd = um.enum_values();
I can just iterate across the map and collect the result, but is there a more efficient method? It would be nice to have a method that also works for regular map, since I might switch to that.
我可以遍历地图并收集结果,但有没有更有效的方法?有一种也适用于常规地图的方法会很好,因为我可能会切换到该方法。
回答by Keith Layne
Okay, here you go:
好的,给你:
std::vector<Key> keys;
keys.reserve(map.size());
std::vector<Val> vals;
vals.reserve(map.size());
for(auto kv : map) {
keys.push_back(kv.first);
vals.push_back(kv.second);
}
Efficiency can probably be improved, but there it is. You're operating on two containers though, so there's not really any STL magic that can hide that fact.
效率可能会得到提高,但就是这样。不过,您在两个容器上进行操作,因此实际上没有任何 STL 魔法可以隐藏这一事实。
As Louis said, this will work for any of the STL map
or set
containers.
正如路易斯所说,这适用于任何 STLmap
或set
容器。
回答by Marius Renn
Using C++-14 you could also do the following (edited to contain full source):
使用 C++-14,您还可以执行以下操作(已编辑为包含完整源代码):
#include <algorithm>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef string Key;
typedef int Value;
auto key_selector = [](auto pair){return pair.first;};
auto value_selector = [](auto pair){return pair.second;};
int main(int argc, char** argv) {
// Create a test map
unordered_map<Key, Value> map;
map["Eight"] = 8;
map["Ten"] = 10;
map["Eleven"] = 11;
// Vectors to hold keys and values
vector<Key> keys(map.size());
vector<Value> values(map.size());
// This is the crucial bit: Transform map to list of keys (or values)
transform(map.begin(), map.end(), keys.begin(), key_selector);
transform(map.begin(), map.end(), values.begin(), value_selector);
// Make sure this worked: Print out vectors
for (Key key : keys) cout << "Key: " << key << endl;
for (Value value : values) cout << "Value: " << value << endl;
return 0;
}
I compiled this with the following command:
我用以下命令编译了它:
g++ keyval.cpp -std=c++14 -o keyval
Testing it printed the keys and values as expected.
测试它按预期打印了键和值。
回答by Louis
In STL there is no built-in method to get all keys or values from a map.
在 STL 中,没有从映射中获取所有键或值的内置方法。
There is no different to iterate a unordered map or regular map, the best way is to iterate it and collect key or value to a vector.
迭代无序映射或常规映射没有什么不同,最好的方法是迭代它并将键或值收集到向量中。
You can write a template function to iterate any kind of map.
您可以编写模板函数来迭代任何类型的地图。
回答by elimad
Joining late, but thought this might be helpful to someone.
Two template functions making use of key_type
and mapped_type
.
加入晚了,但认为这可能对某人有帮助。
使用key_type
和 的两个模板函数mapped_type
。
namespace mapExt
{
template<typename myMap>
std::vector<typename myMap::key_type> Keys(const myMap& m)
{
std::vector<typename myMap::key_type> r;
r.reserve(m.size());
for (const auto&kvp : m)
{
r.push_back(kvp.first);
}
return r;
}
template<typename myMap>
std::vector<typename myMap::mapped_type> Values(const myMap& m)
{
std::vector<typename myMap::mapped_type> r;
r.reserve(m.size());
for (const auto&kvp : m)
{
r.push_back(kvp.second);
}
return r;
}
}
Usage:
用法:
std::map<long, char> mO;
std::unordered_map<long, char> mU;
// set up the maps
std::vector<long> kO = mapExt::Keys(mO);
std::vector<long> kU = mapExt::Keys(mU);
std::vector<char> vO = mapExt::Values(mO);
std::vector<char> vU = mapExt::Values(mU);