Is the following code correct?

#include <vector>
#include <iostream>

using namespace std;

int main() {
    vector<int> v;
    v.resize(5);
    vector<int>::iterator i = v.begin();
    *i = 5;
    v.resize(10);
    cout << "v.begin() = " << *i << endl;
}
Explanation
After the second vector v resize operation the memory for its storage may be allocated starting from the new address, so there is no guarantee that the iterator that is stored in the i variable points to the correct memory area. Runtime error is possible / undefined behavior.

Слідкуй за CodeGalaxy

Мобільний додаток Beta

Get it on Google Play
Зворотній Зв’язок
Cosmo
Зареєструйся Зараз
або Підпишись на майбутні тести