forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex9_33.cpp
36 lines (31 loc) · 904 Bytes
/
ex9_33.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//
// ex9_33.cpp
// Exercise 9.33
//
// Created by pezy on 12/3/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// @Brief In the final example in this section what would happen
// if we did not assign the result of insert to begin?
// Write a program that omits this assignment to see if your
// expectation was correct.
// @Answer crash, cause the iterator is invalid after insert. Because
// Iterators, pointers, and references to a
// vector or string are invalid if the container was reallocated.
#include <iostream>
#include <vector>
using std::vector;
using std::cout;
using std::endl;
int main()
{
vector<int> v{1, 2, 3, 4, 5, 6, 7, 8, 9};
auto begin = v.begin();
while (begin != v.end()) {
++begin;
/*begin = */ v.insert(begin, 42);
++begin;
}
for (auto i : v) cout << i << " ";
return 0;
}