无序集的编译问题 我试图从C++ STD库中使用 unordeDeSET/。我正在使用std名称空间 using namespace std;
无序集的编译问题 我试图从C++ STD库中使用 unordeDeSET/。我正在使用std名称空间 using namespace std;,c++,c++11,unordered-set,C++,C++11,Unordered Set,无序集属于我的功能范围。我想用它来记忆一些价值观 int do_crazy_calculations(int n) { static unordered_set<int> done_before; done_before::iterator node_found = done_before.find(n); // n has not been seen before, so do calculations and memoize the result.
无序集
属于我的功能范围。我想用它来记忆一些价值观
int do_crazy_calculations(int n) {
static unordered_set<int> done_before;
done_before::iterator node_found = done_before.find(n);
// n has not been seen before, so do calculations and memoize the result.
if (node_found == done_before.end()) {
int result = actually_do_calculations(n);
done_before.insert(n, result);
return result;
}
// n has already been seen before, just return the memoized value.
else {
return node_found.get();
}
}
int do\u疯狂计算(int n){
静态无序设置之前已完成;
完成之前::迭代器节点\u found=done\u before.find(n);
//n以前从未见过,计算和记录结果也是如此。
if(node_found==done_before.end()){
int结果=实际计算(n);
之前完成。插入(n,结果);
返回结果;
}
//n之前已经出现过,只需返回已记忆的值即可。
否则{
返回节点_found.get();
}
}
但是,我遇到了以下编译错误:
cpluplusexperience.cpp:在函数'int do\u craze\u calculations(int)
:cpluplusexperience.cpp:10:10:错误:
“无序集”
未命名类型make:**[cpluplusexperiment.o]错误1
但是,我确实为
无序集-int
分配了一个类型,对吗?这个错误是什么意思?应该是无序的\u集::迭代器节点\u found=…
我通常使用typedef来简化模板变量的命名:
typedef unordered_set<int> t_done_before;
static t_done_before done_before;
t_done_before::iterator node_found = ...
typedef无序\u设置\u完成\u之前;
静态t_done_before done_before;
t_done_before::迭代器节点_found=。。。
首先,千万不要使用名称空间std——这会导致上千个令人沮丧的错误李>
done\u此前
实际上并没有命名类型,而是命名变量。要命名类型,可以在类型之前使用typedef unordered\u set done\u
,然后在类型之前使用done\u::iterator
您需要包括标题
最后,您需要一个支持它的编译器(VS2010+,GCC4.4+)或通过Boost或TR1库正确使用
首先,无序_集在TR1或C++11中
第二,在函数中声明集合,然后测试其中的某些值。重点是什么?每次你调用函数时,集合都是空的。编辑:抱歉,没有注意到它是静态的。您是否使用了-std=c++0x
进行编译?std::无序集::insert
不需要两个int
,只需要一个。也许你在想std::map
?谢谢,这是需要纠正的。同样的编译错误也在发生,但它是静态的,对吗?它不应该在函数调用中保持状态吗?