Do not treat the size_type as int or unsiged, because size_type aren't always int or unsigned int, double, etc.

For example.

vector

v.size(); // return 0;

v.size() -1 ; // return the largest unsigned value of integer type

// because there is no such thing as "-1" for unsigned int

so, we cannot just directly use the size_type as index.

//whoops! if v is empty, v.size() -1 is 4,294,967,295 on 32 big machine

or 18446744073709551615 on a 64 big machine

for(int i = 0; i < v.size() -1; i++)

you can do a cast so you can work on them as a normal index

for (int i =0; i <>(v.size())-1; i++)

or directly check them like

if (s.find("hello") == std::string::npos)

## No comments:

Post a Comment