Search code examples
c++vectorsize

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


The following code

#include <vector>
#include <iostream>
using namespace std;
int main() {
    vector<int> value;
    cout << value.size() << endl;  // output 0
    cout << value.size() - 1 << endl;  // output 18446744073709551615
}

Why the second output is not -1? What happens at the second cout?


Solution

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