xcrypt
xcrypt

Reputation: 3376

warning on std::vector constructor

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?

Upvotes: 1

Views: 377

Answers (1)

Seth Carnegie
Seth Carnegie

Reputation: 75130

It's creating a "2-dimensional" vector. It has rows number of rows, and each row has cols number of columns, and each cell is initialised to false (0).

The constructor of vector that 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. So visited initially has rows elements, and each one is initialised with std::vector<bool>(cols, 0), which initially has cols number of elements, and each one is initialised with 0 (which is false).

It's giving you that warning because it's converting 0, an integer, to to false, a bool. You can fix it by replacing 0 with false.

Upvotes: 7

Related Questions