The following code
#include
#include
using namespace std;
int main() {
vector value;
cout << value.
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 and define
using Size = ptrdiff_t;
and further (the second function here requires inclusion of
template< class Type >
auto n_items( Type const& o )
-> Size
{ return o.size(); }
template< Size n >
auto n_items( std::bitset const& o )
-> Size
{ return o.count(); } // Corresponds to std::set::size()
Then you can write
n_items( v )
and get a signed integer result, and -1 when you subtract 1 from 0.