C++ 为什么这个代码可以工作

C++ 为什么这个代码可以工作,c++,C++,我刚刚发现这个代码实际上是有效的。我想知道为什么。行和列都是变量。我试图动态分配一个2D向量。有人能解释一下吗?谢谢 行和列都是“int” 网格=新向量(行、列); 对于std::vector,有几种方法。您尚未提供行和列的类型,但我怀疑您的代码不会达到预期的效果。如果您想在二维中初始化一个向量,那么您需要一个构造函数,该构造函数接受一个大小和一个值来初始化每个元素。在这种情况下,该值本身就是一个向量 int row = 5; std::vector<bool> col(5, fal

我刚刚发现这个代码实际上是有效的。我想知道为什么。行和列都是变量。我试图动态分配一个2D向量。有人能解释一下吗?谢谢

行和列都是“int”

网格=新向量(行、列);
对于
std::vector
,有几种方法。您尚未提供
的类型,但我怀疑您的代码不会达到预期的效果。如果您想在二维中初始化一个向量,那么您需要一个构造函数,该构造函数接受一个大小和一个值来初始化每个元素。在这种情况下,该值本身就是一个向量

int row = 5;
std::vector<bool> col(5, false);
grid_ = std::vector<std::vector<bool>>(row, col);
int行=5;
标准::向量列(5,假);
网格=标准::向量(行,列);
这将初始化一个5x5的布尔网格,所有这些网格都设置为
false

有几个用于
std::vector
。您尚未提供
的类型,但我怀疑您的代码不会达到预期的效果。如果您想在二维中初始化一个向量,那么您需要一个构造函数,该构造函数接受一个大小和一个值来初始化每个元素。在这种情况下,该值本身就是一个向量

int row = 5;
std::vector<bool> col(5, false);
grid_ = std::vector<std::vector<bool>>(row, col);
int行=5;
标准::向量列(5,假);
网格=标准::向量(行,列);

这将初始化一个5x5的布尔网格,在给定原始代码的情况下,所有布尔都设置为
false

grid_ = new vector<vector<bool> > (row, col);  
[D:\dev\test] >g++foo.cpp 在d:\bin\mingw\bin\../lib/gcc/i686-pc-mingw32/4.7.2/../../../../../../include/c++/4.7.2/vector:65:0中包含的文件中, 来自foo.cpp:1: d:\bin\mingw\bin\../lib/gcc/i686-pc-mingw32/4.7.2/../../../../include/c++/4.7.2/bits/stl\u vector.h:void std::vector::\u M\u initialize\u dispatch(\u Integer,\u Integer,std:\u true\u type)[with\u Integer=int;\u Tp std::vector;\u Alloc=std::alloct]的实例化 或>]': d:\bin\mingw\bin\../lib/gcc/i686-pc-mingw32/4.7.2/../../../../../../include/c++/4.7.2/bits/stl\u vector.h:393:4:从'std::vector::vector(\u InputIterator,\u InputIterator,const allocator\u type&)][with\u InputIterator=int;\u Tp std::vector;\u Alloc=std::all]中需要 ocator>;std::vector::allocator_type=std::allocator>]' foo.cpp:8:49:从这里开始需要 d:\bin\mingw\bin\../lib/gcc/i686-pc-mingw32/4.7.2/../../../../../../include/c++/4.7.2/bits/stl_vector.h:1137:4:错误:ca没有匹配函数 ll至'std::vector>::_M_fill_initialize(std::vector>::size_type,int&)' d:\bin\mingw\bin\../lib/gcc/i686-pc-mingw32/4.7.2/../../../../../../../include/c++/4.7.2/bits/stl_vector.h:1137:4:注:候选者为: d:\bin\mingw\bin\../lib/gcc/i686-pc-mingw32/4.7.2/../../../include/c++/4.7.2/bits/stl_vector.h:1179:7:注意:void std::vector::_M_fill_initialize(std::vector::size_type,const value_type&)[带(Tp std::vector;(Alloc=std::分配器>)std::vector::size(size)type=unsigned int;std::vector::value)type=std] d:\bin\mingw\bin\../lib/gcc/i686-pc-mingw32/4.7.2/../../../../../../include/c++/4.7.2/bits/stl_vector.h:1179:7:注意:ar的转换未知 gument 2从'int'到'const value_type&{aka const std::vector&} [D:\dev\test] > _
根据原始代码

grid_ = new vector<vector<bool> > (row, col);  
[D:\dev\test] >g++foo.cpp 在d:\bin\mingw\bin\../lib/gcc/i686-pc-mingw32/4.7.2/../../../../../../include/c++/4.7.2/vector:65:0中包含的文件中, 来自foo.cpp:1: d:\bin\mingw\bin\../lib/gcc/i686-pc-mingw32/4.7.2/../../../../include/c++/4.7.2/bits/stl\u vector.h:void std::vector::\u M\u initialize\u dispatch(\u Integer,\u Integer,std:\u true\u type)[with\u Integer=int;\u Tp std::vector;\u Alloc=std::alloct]的实例化 或>]': d:\bin\mingw\bin\../lib/gcc/i686-pc-mingw32/4.7.2/../../../../../../include/c++/4.7.2/bits/stl\u vector.h:393:4:从'std::vector::vector(\u InputIterator,\u InputIterator,const allocator\u type&)][with\u InputIterator=int;\u Tp std::vector;\u Alloc=std::all]中需要 ocator>;std::vector::allocator_type=std::allocator>]' foo.cpp:8:49:从这里开始需要 d:\bin\mingw\bin\../lib/gcc/i686-pc-mingw32/4.7.2/../../../../../../include/c++/4.7.2/bits/stl_vector.h:1137:4:错误:ca没有匹配函数 ll至'std::vector>::_M_fill_initialize(std::vector>::size_type,int&)' d:\bin\mingw\bin\../lib/gcc/i686-pc-mingw32/4.7.2/../../../../../../../include/c++/4.7.2/bits/stl_vector.h:1137:4:注:候选者为: d:\bin\mingw\bin\../lib/gcc/i686-pc-mingw32/4.7.2/../../../include/c++/4.7.2/bits/stl_vector.h:1179:7:注意:void std::vector::_M_fill_initialize(std::vector::size_type,const value_type&)[带(Tp std::vector;(Alloc=std::分配器>)std::vector::size(size)type=unsigned int;std::vector::value)type=std] d:\bin\mingw\bin\../lib/gcc/i686-pc-mingw32/4.7.2/../../../../../../include/c++/4.7.2/bits/stl_vector.h:1179:7:注意:ar的转换未知 gument 2从'int'到'const value_type&{aka const std::vector&} [D:\dev\test] > _
为什么要动态分配一个2D向量?@Rapptz有什么理由不这样做吗?这段代码甚至没有compile@AlanShore是的,避免使用指针有很多原因,特别是指向向量的指针。如果你想让我们给出可靠的答案,你应该输入
的类型。你为什么要动态分配一个2D向量?@Rapptz有什么理由不这样做吗?这段代码甚至没有compile@AlanShore是的,避免使用指针有很多原因,特别是指向向量的指针。如果希望我们能够给出可靠的答案,您应该输入
的类型。谢谢。但如果我想将“网格”初始化为所有“false”。有没有更好的办法?而不是动态分配?@anonymous downvoter:请解释您的downvote,以便其他人可以从您的见解中受益。谢谢。但如果我想将“网格”初始化为所有“false”。有没有更好的办法?而不是动态分配?@anonymous downvoter:请解释您的downvote,以便其他人可以从您的见解中受益。 [D:\dev\test] > g++ foo.cpp In file included from d:\bin\mingw\bin\../lib/gcc/i686-pc-mingw32/4.7.2/../../../../include/c++/4.7.2/vector:65:0, from foo.cpp:1: d:\bin\mingw\bin\../lib/gcc/i686-pc-mingw32/4.7.2/../../../../include/c++/4.7.2/bits/stl_vector.h: In instantiation of 'void std::vector::_M_initialize_dispatch(_Integer, _Integer, std::__true_type) [with _Integer = int; _Tp = std::vector; _Alloc = std::allocat or >]': d:\bin\mingw\bin\../lib/gcc/i686-pc-mingw32/4.7.2/../../../../include/c++/4.7.2/bits/stl_vector.h:393:4: required from 'std::vector::vector(_InputIterator, _InputIterator, const allocator_type&) [with _InputIterator = int; _Tp = std::vector; _Alloc = std::all ocator >; std::vector::allocator_type = std::allocator >]' foo.cpp:8:49: required from here d:\bin\mingw\bin\../lib/gcc/i686-pc-mingw32/4.7.2/../../../../include/c++/4.7.2/bits/stl_vector.h:1137:4: error: no matching function for ca ll to 'std::vector >::_M_fill_initialize(std::vector >::size_type, int&)' d:\bin\mingw\bin\../lib/gcc/i686-pc-mingw32/4.7.2/../../../../include/c++/4.7.2/bits/stl_vector.h:1137:4: note: candidate is: d:\bin\mingw\bin\../lib/gcc/i686-pc-mingw32/4.7.2/../../../../include/c++/4.7.2/bits/stl_vector.h:1179:7: note: void std::vector::_M_fill_initialize(std::vector::size_type, const value_type&) [with _Tp = std::vector; _Alloc = std::allocator >; std::vector::size_type = unsigned int; std::vector::value_type = std::vector] d:\bin\mingw\bin\../lib/gcc/i686-pc-mingw32/4.7.2/../../../../include/c++/4.7.2/bits/stl_vector.h:1179:7: note: no known conversion for ar gument 2 from 'int' to 'const value_type& {aka const std::vector&}' [D:\dev\test] > _