c++ - std::vector 中没有名为 iterator_category 的类型

标签 c++ vector

这段代码出错了:

vector<vector<bool> > revealed(vector<bool>(10, false), vector<bool>(10,false));

我正在尝试定义一个默认所有元素都为 false 的二维 bool 数组。

错误是

.\stl_iterator_base_types.h|165|error: no type named 'iterator_category' in 'class std::vector<bool>'|

错误指的是STL_iterator_base_types.h165行

162 template<typename _Iterator>
163 struct iterator_traits
164 {
165     typedef typename _Iterator::iterator_category iterator_category;
166     typedef typename _Iterator::value_type        value_type;
167     typedef typename _Iterator::difference_type   difference_type;
168     typedef typename _Iterator::pointer           pointer;
169     typedef typename _Iterator::reference         reference;
170 };

顺便说一下,我使用 Code::Blocks(with MinGW) 作为我的 IDE

最佳答案

您可以按如下方式初始化这样的 vector

std::vector<std::vector<bool>> revealed(10, std::vector<bool>(10, false));

原因是您尝试用于 std::vector 的构造函数重载是

vector(size_type count, const T& value);

所以你可以看到第一个参数是计数,第二个是值。根据这个概念,您希望外部 vector 是

vector(10, "vectors_of_length_10_wlth_all_false_values")
           ^

你表示第二个参数的方式是

std::vector<bool>(10, false)

外部 vector 的第一个参数只是 10,因为您希望它包含 10 个具有 10 个 false 值的 vector 。

关于c++ - std::vector 中没有名为 iterator_category 的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42555813/

相关文章:

c++ - Opengl 矩形不会渲染

c - Read raw C string to Rust...在此上下文中将有符号转换为无符号的正确方法是什么?

c++ - 将 vector<int32_t> 转换为 int[] 的最佳方法是什么

c++ - 'vectorize' 这个重复的 C++ 代码的方法?

c++ - QString::utf16(): 是不是UB?

c++ - 不确定如何将 opencv 库添加到 CMakeLists.txt

c# - Oculus Rift,如何在 Visual Studio 中添加库

c++ - 将 nullptr 转换为 std::optional

c++ - 作为函数参数传递的 vector 初始值设定项列表

c++ - std::vector 在 push_back 期间多次调用析构函数?