I’m trying to figure out what this piece of code does:
std::vector<std::vector<bool> > visited(rows, std::vector<bool>(cols, 0));
‘rows’ and ‘cols’ are both integers.
It calls the constructor, but I’m not sure how.
It’s sample code I got from some project…
It’s also giving me the following warning:
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> ]
Could anyone help?
It’s creating a “2-dimensional” vector. It has
rowsnumber of rows, and each row hascolsnumber of columns, and each cell is initialised tofalse(0).The constructor of
vectorthat it’s using is the one that takes the number of elements it should have initially, along with what value to initialise each element with. Sovisitedinitially hasrowselements, and each one is initialised withstd::vector<bool>(cols, 0), which initially hascolsnumber of elements, and each one is initialised with0(which isfalse).It’s giving you that warning because it’s converting
0, an integer, to tofalse, abool. You can fix it by replacing0withfalse.