C/C++ use of int or unsigned int

后端 未结 6 1310
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-01 09:17

In a lot of code examples, source code, libraries etc. I see the use of int when as far as I can see, an unsigned int would make much more sense.

One pl

6条回答
  •  盖世英雄少女心
    2021-01-01 10:09

    this tiny code is usecase related, if you call some vector element then the prototype is int but there're much modern ways to do it in c++ eg. for(const auto &v : vec) {} or iterators, in some calculcation if there's no substracting/reaching a negative number you can and should use unsigned (explains better the range of values expected), sometimes as many posted examples here shows you actually need int but the truth is it's all about usecase and situation, no one strict rule apply to all usecases and it would be kinda dumb to force one over...

提交回复
热议问题