vector size - 1 when size is 0 in C++

后端 未结 5 2172
渐次进展
渐次进展 2020-11-28 16:48

The following code

#include 
#include 
using namespace std;
int main() {
    vector value;
    cout << value.         


        
相关标签:
5条回答
  • 2020-11-28 17:07

    The output is automatically casted to size_t because that's the return type of value.size(), which is an unsigned type. Hence you see an unsigned value printed.

    0 讨论(0)
  • 2020-11-28 17:16

    vector::size() is of type size_t which is an unsigned type, and unsigned integers can't represent negative numbers.

    0 讨论(0)
  • 2020-11-28 17:18

    value.size() returns an unsigned type, so by doing -1 you are actually doing an overflow

    0 讨论(0)
  • 2020-11-28 17:24

    The .size() returns a 'size_t' type that is a unsigned int. The second output is the maximum integer of your machine.

    0 讨论(0)
  • 2020-11-28 17:25

    Unsigned integer types in C++ do “wrap around arithmetic” a.k.a. clock arithmetic a.k.a. modulo arithmetic. And the result of any standard library size function is unsigned, usually the type size_t. And so, when you subtract 1 from 0 of type size_t, you get the largest size_t value.

    To avoid these problems you can include <stddef.h> and define

    using Size = ptrdiff_t;
    

    and further (the second function here requires inclusion of <bitset),

    template< class Type >
    auto n_items( Type const& o )
        -> Size
    { return o.size(); }
    
    template< Size n >
    auto n_items( std::bitset<n> const& o )
        -> Size
    { return o.count(); }       // Corresponds to std::set<int>::size()
    

    Then you can write

    n_items( v )
    

    and get a signed integer result, and -1 when you subtract 1 from 0.

    0 讨论(0)
提交回复
热议问题