Should unsigned ints be used if not necessary?

前端 未结 7 1064
醉梦人生
醉梦人生 2020-12-15 05:38

Should one ever declare a variable as an unsigned int if they don\'t require the extra range of values? For example, when declaring the variable in a for loop, if you know i

7条回答
  •  误落风尘
    2020-12-15 06:40

    int is the general purpose integer type. If you need an integer, and int meets your requirements (range [-32767,32767]), then use it.

    If you have more specialized purposes, then you can choose something else. If you need an index into an array, then use size_t. If you need an index into a vector, then use std::vector::size_type. If you need specific sizes, then pick something from . If you need something larger than 64 bits, then find a library like gmp.

    I can't think of any good reasons to use unsigned int. At least, not directly (size_t and some of the specifically sized types from may be typedefs of unsigned int).

提交回复
热议问题