我可以通过整数索引访问 c++ std::map 中的元素吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6919140/
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
Can I access the elements in a c++ std::map by an integer index?
提问by Charles
I have a map of elements that I would like to iterate through. Of course, the standard way to do that would be using a for loop with
我有一张我想要遍历的元素地图。当然,这样做的标准方法是使用 for 循环
for (map<string, int> iterator it = myMap.begin(); it != myMap.end(); ++it) {
string thisKey = it->first;
int thisValue = it->second;
}
but if I try and make this loop run parallel using OpenMP's parallel forconstruct, it doesn't work, and this is (apparently) a known issue, as it doesn't recognize this sort of loop construct.
但是,如果我尝试使用 OpenMP 的并行构造使该循环并行运行,则它不起作用,并且这(显然)是一个已知问题,因为它无法识别这种循环构造。
So, my backup plan was to use an integer index iterator, and access the list of keys and values by index, as I would do in C# like so:
所以,我的备份计划是使用整数索引迭代器,并按索引访问键和值的列表,就像我在 C# 中所做的那样:
for (int i = 0; i < myMap.Count; ++i) {
string thisKey = myMap.Keys[i];
string thisValue = myMap.Values[i];
}
... yet I can't seem to find an equivalent method in C++. Is there a way to do this in C++ that I'm unaware of?
...但我似乎无法在 C++ 中找到等效的方法。有没有办法在我不知道的 C++ 中做到这一点?
回答by Rob?
I don't know anything about OpenMP, so I don't know if it will optimize the following or not. But you could use std::advance
, like so:
我对OpenMP一无所知,所以我不知道它是否会优化以下内容。但是你可以使用std::advance
,像这样:
#include <map>
#include <string>
#include <iterator>
#include <iostream>
typedef std::map<std::string, int> Map;
int main() {
Map m;
m["one"] = 1;
m["two"] = 2;
for(int i = 0; i < m.size(); ++i) {
Map::iterator it = m.begin();
std::advance(it, i);
std::string thiskey = it->first;
int thisValue = it->second;
std::cout << thiskey << "\n";
}
}
But do be aware that std::advance
is O(n), so your (single-threaded) complexity is O(n^2).
但请注意,这std::advance
是 O(n),因此您的(单线程)复杂性是 O(n^2)。
EDIT编辑:如果你将地图元素复制到一个向量,意识到你可以在一个声明中做到这一点:
std::vector<Map::value_type> v(m.begin(), m.end());
thus:
因此:
#include <map>
#include <string>
#include <iterator>
#include <iostream>
#include <vector>
typedef std::map<std::string, int> Map;
int main() {
Map m;
m["one"] = 1;
m["two"] = 2;
int i = 0;
for( std::vector<Map::value_type> v(m.begin(), m.end());
i < v.size(); ++i) {
std::string thiskey = v[i].first;
int thisValue = v[i].second;
std::cout << thiskey << "\n";
}
}
回答by Tom Kerr
Here are a few options that are relativelypainless.
以下是一些相对无痛的选项。
Keep a
std::vector
orstd::deque
for array access, and a separate map of values. The leg work of ensuring that they are consistent is yourproblem.Use boost::multi_indexto ensure consistency between the two index structures. As a word of warning, compile times are pretty long with this option. Consider using the pimpl idiomif you go this route.
保留一个
std::vector
orstd::deque
用于数组访问,以及一个单独的值映射。确保它们一致的腿部工作是您的问题。使用boost::multi_index来确保两个索引结构之间的一致性。作为警告,这个选项的编译时间很长。如果你走这条路,请考虑使用pimpl 成语。
I have no experience with OpenMP, so I cannot speculate if either of these options would be worthwhile in practice.
我没有使用 OpenMP 的经验,因此我无法推测这些选项中的任何一个在实践中是否值得。