[英]warning on std::vector constructor
我試圖弄清楚這段代碼的作用:
std::vector<std::vector<bool> > visited(rows, std::vector<bool>(cols, 0));
“ rows”和“ 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> ]
有人可以幫忙嗎?
它正在創建“二維”矢量。 它的rows
數為rows
數,每行的列數為cols
,並且每個單元格都初始化為false
( 0
)。
它使用的vector
的構造函數是一個使用其初始應具有的元素數量以及用來初始化每個元素的值的構造函數。 因此, visited
最初具有rows
元素,並且每個元素都以std::vector<bool>(cols, 0)
初始化,該元素最初具有cols
元素數量,並且每個元素都由0
初始化(為false
)。
之所以發出警告,是因為它將0
(一個整數)轉換為false
(一個bool
。 您可以通過將0
替換為false
來修復它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.