簡體   English   中英

在std :: vector構造函數上警告

[英]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 ,並且每個單元格都初始化為false0 )。

它使用的vector的構造函數是一個使用其初始應具有的元素數量以及用來初始化每個元素的值的構造函數。 因此, visited最初具有rows元素,並且每個元素都以std::vector<bool>(cols, 0)初始化,該元素最初具有cols元素數量,並且每個元素都由0初始化(為false )。

之所以發出警告,是因為它將0 (一個整數)轉換為false (一個bool 您可以通過將0替換為false來修復它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM