c++c++11size-type

vector<vector<float>>::size_type or vector<float>::size_type when dealing with vector<vector<float>>'s size()?


code_1:

std::vector< std::vector<float> > grid (5, std::vector<float>(3, 1));
std::vector< std::vector <float> >::size_type rows = grid.size();

or

code_2:

std::vector< std::vector<float> > grid (5, std::vector<float>(3, 1));
std::vector<float>::size_type rows = grid.size();

Should I use code_1 or code_2 ? and why ? thank you !


Solution

  • Given that std::vector< std::vector<float>>::size returns value of type std::vector< std::vector <float>>::size_type, why would you use it to initialise a variable of type std::vector<float>::size_type?

    That said, those are the same type, so it makes little difference. The type is std::size_t, so you might as well use std::size_t rows. Or, if you want to avoid making a mistake, let the compiler deduce the type for the variable by using auto.