mercredi 11 août 2021

After vec.push_back, why can I do access index that is out of range?

#include <vector>
#include <iostream>

using namespace std;

vector<int> vec;

int main() {
    vec.push_back(1);
    cout << vec[100];
}

this output it 0

but,

#include <vector>
#include <iostream>

using namespace std;

vector<int> vec;

int main() {
    cout << vec[100];
}

this is error that is 'out of range'

so In case after vec.push_back(1), I wonder why I can access 100 index.

Aucun commentaire:

Enregistrer un commentaire