2011-11-27 62 views
1

我试图搞清楚这段代码的作用:警告上的std ::矢量构造

std::vector<std::vector<bool> > visited(rows, std::vector<bool>(cols, 0)); 

“行”和“的cols”都是整数。

它调用构造函数,但我不知道如何。 它的样本代码,我从一些项目得到了...

它也给了我以下警告:

c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector(2140): warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning) 
1>   c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector(2126) : see reference to function template instantiation 'void std::vector<_Ty,_Ax>::_BConstruct<_Iter>(_Iter,_Iter,std::_Int_iterator_tag)' being compiled 
1>   with 
1>   [ 
1>    _Ty=bool, 
1>    _Ax=std::allocator<bool>, 
1>    _Iter=int 
1>   ] 
1>   c:\ai challenge\code\project\project\state.cpp(85) : see reference to function template instantiation 'std::vector<_Ty,_Ax>::vector<int>(_Iter,_Iter)' being compiled 
1>   with 
1>   [ 
1>    _Ty=bool, 
1>    _Ax=std::allocator<bool>, 
1>    _Iter=int 
1>   ] 

谁能帮助?

回答

7

它创建了一个“二维”向量。它有rows的行数,每行有cols列数,每个单元初始化为false0)。

它使用的构造函数是最初应该具有的元素数量以及初始化每个元素的值。因此visited最初有rows元素,并且每个元素都被初始化为std::vector<bool>(cols, 0),它最初有cols个元素,并且每个元素都被初始化为0(即false)。

由于它将0(整数)转换为false,bool,因此它会给出警告。您可以通过将0替换为false来修复它。

+0

谢谢,那帮了:) – xcrypt