无序集的编译问题
compilation problems with unordered set
我正在尝试使用C++标准库中的unordered_set
。我使用的是std命名空间。
using namespace std;
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.
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();
}
}
然而,我得到了这个编译错误:
CplusplusExperiment.cpp:在函数
'int do_crazy_calculations(int)'
中:
CplusplusExperiment.cpp:10:10:错误:'unordered_set'
未命名类型
制造商:***[CplusplusExperiment.o]错误1
但是,我确实为unordered_set
-int
分配了一个类型,对吗?这个错误是什么意思?
- 首先,永远不要做
using namespace std
——它是一千个令人沮丧的错误的来源 done_before
实际上并没有命名类型,而是命名变量。要命名一个类型,可以使用typedef unordered_set<int> done_before_type
,然后done_before_type::iterator
就可以了- 您需要包括标题
<unordered_set>
- 最后,您需要一个支持它的编译器(VS 2010+,GCC 4.4+),或者通过Boost或TR1库进行正确使用
应为unordered_set<int>::iterator node_found = ...
我通常使用typedef来简化模板变量的命名:
typedef unordered_set<int> t_done_before;
static t_done_before done_before;
t_done_before::iterator node_found = ...
首先,unordered_set在TR1或C++11中。
其次,在函数中声明集合,然后测试其中的一些值。这有什么意义?每次调用函数时,集合都将为空。编辑:对不起,没有注意到它是静态的。
相关文章:
- 编译包含字符串的代码时遇到问题
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 编译要在英特尔Hyperscan中使用的.cc文件时出现问题
- 如何在 Mac 上正确编译C++,当它在 Linux 上编译没有问题时?
- Rextester 语言编译C++问题
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 编译 llvm 插件时出现问题:llvm/Config/llvm-config.h:没有这样的文件或目录
- C++ 多级虚拟继承编译问题
- 编译问题:在函数"_start"中:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 在Linux上使用Clang / OLLVM交叉编译helloworld Windows可执行文件时的问题
- 使用 CImg 库的 std::min 和 std::max 的编译问题
- Qt Visual Studio 2015 加载项编译问题
- 构造函数和 G++ 编译配方的问题
- 解析问题 - 预期的非限定 ID - #include <array> 编译错误
- libssh 问题编译问题未定义的错误
- Eclipse 问题 - 编译期间不考虑 .c 和 .cpp 文件中定义的预处理
- Visual Studio 2017问题编译了C 代码,而该代码则在Linux中编译
- 这个C++代码有什么问题?编译错误:调用“测试::测试(测试)”没有匹配函数
- 试图编译基本Boost PropertyTree示例时出现问题-编译错误.该怎么做
- 如何解决***缺少分隔符的问题.编译QT(或支持库)时