Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

vector

size

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?

like image 950
ZigZagZebra Avatar asked Dec 11 '22 19:12

ZigZagZebra


1 Answers

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

like image 196
Jonathan Potter Avatar answered Dec 20 '22 19:12

Jonathan Potter