I am writing this answer because the other two, including the accepted one, are wrong. The type std::vector size() not unsigned int , not size_t .
The size type of std::vector<T> is equal to std::vector<T>::size_type .
What is it. In some architectures and for some compilers, this may be the same as size_t , but in some others it may not. The assumption that a variable of type size_t may contain the same values โโas one of the types std::vector<T>::size_type may fail.
To check that your vector is the right size, you can do something like:
if(vec.size() != static_cast<std::vector<int>::size_type>(expected_size)) { std::cerr << "Error!" << std::endl; }
source share