Jump to content
  • Advertisement
Sign in to follow this  
Sfpiano

STL Signed/Unsigned mismatch

This topic is 4691 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Say I'm trying to loop through an std::vector. I might have code like this:
for(int i=0; i<vec.size(); i++) {
...
}
Then the compiler gives me a warning about the comparison between the signed i and unsigned vec.size(). Throughout the course of my code I have about 100 of these warnings and I was wondering whether or not they had any real bearing on anything.

Share this post


Link to post
Share on other sites
Advertisement
When just looping those warnings are usually benign, but you should consider switching to a unsigned variable anyways. Alterantely, you can use iterators to loop through the vector.

Share this post


Link to post
Share on other sites
Hello !

You can write

for (std::vector<mytype>::size_type i=0; i<vec.size(); i++) {
...
}

Or (simpler, since finnaly ti will resolve to the same thing)

for (size_t i=0; i<vec.size(); i++) {
...
}

The vector<>::size() methode returns an unsigned int (it makes not sense to return a signed one).

But unless you are dealing with very large vectors (more than 231 entries), your code will work correctly.

(edit: C'mon, SiCrane, type slower please! [smile])

HTH,

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!