Cha*_*les 9 c++ dictionary iterator stdmap openmp
我有一个我想要迭代的元素图.当然,标准的方法是使用for循环
for (map<string, int> iterator it = myMap.begin(); it != myMap.end(); ++it) {
string thisKey = it->first;
int thisValue = it->second;
}
Run Code Online (Sandbox Code Playgroud)
但是如果我尝试使用OpenMP的并行构造使这个循环并行运行,它就不起作用,这显然是一个已知的问题,因为它不能识别这种循环结构.
所以,我的备份计划是使用整数索引迭代器,并按索引访问键和值列表,就像我在C#中所做的那样:
for (int i = 0; i < myMap.Count; ++i) {
string thisKey = myMap.Keys[i];
string thisValue = myMap.Values[i];
}
Run Code Online (Sandbox Code Playgroud)
...但我似乎无法在C++中找到一个等效的方法.有没有办法在C++中做到这一点,我不知道?
我对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";
}
}
Run Code Online (Sandbox Code Playgroud)
但请注意,这std::advance是O(n),因此您的(单线程)复杂度为O(n ^ 2).
std::vector<Map::value_type> v(m.begin(), m.end());
Run Code Online (Sandbox Code Playgroud)
从而:
#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";
}
}
Run Code Online (Sandbox Code Playgroud)
以下是一些相对无痛的选项.
保持a std::vector或std::dequefor数组访问,以及单独的值映射.确保它们一致的腿部工作是你的问题.
使用boost :: multi_index来确保两个索引结构之间的一致性.作为警告,使用此选项的编译时间相当长.如果你走这条路,请考虑使用pimpl习语.
我没有使用OpenMP的经验,所以我不能推测这些选项中的任何一个在实践中是否值得.
| 归档时间: |
|
| 查看次数: |
20237 次 |
| 最近记录: |