Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ warning C4018: '<' : signed/unsigned mismatch [duplicate]

Tags:

c++

warnings

Replace all the definitions of int i with size_t i.

std::vector<T>::size() returns the type size_t which is unsigned (since it doesn't make sense for containers to contain a negative number of elements).


Say std::size_t i = 0;:

for (std::size_t i = 0; i != v.size(); ++i) { /* ... */ }

You could also use iterators instead to avoid the potential for a warning altogether:

for (std::vector<int>::const_iterator i = v.begin(); i != v.end(); ++i)
{
    ...
}

Or if you're using C++11:

for (int i : v)
{
    ...
}