1#include <vector>
2
3int main() {
4 std::vector<int> v;
5 v.push_back(10); // v = [10];
6 v.push_back(20); // v = [10, 20];
7
8 v.pop_back(); // v = [10];
9 v.push_back(30); // v = [10, 30];
10
11 auto it = v.begin();
12 int x = *it; // x = 10;
13 ++it;
14 int y = *it; // y = 30
15 ++it;
16 bool is_end = it == v.end(); // is_end = true
17
18 return 0;
19}
1#include <vector>
2#include <string>
3
4int main() {
5 std::vector<std::string> str_v;
6 str_v.push_back("abc");
7 str_v.push_back("hello world!!");
8 str_v.push_back("i'm a coder.");
9 for(auto it = str_v.beigin();it != str_v.end(); it++) {
10 printf("%s\n",it->c_str());
11 }
12}
1#include <vector>
2
3int main() {
4 std::vector<int> myVector = { 666, 1337, 420 };
5
6 size_t size = myVector.size(); // 3
7
8 myVector.push_back(399); // Add 399 to the end of the vector
9
10 size = myVector.size(); // 4
11}
1
2vector<int> vec;
3//Creates an empty (size 0) vector
4
5
6vector<int> vec(4);
7//Creates a vector with 4 elements.
8
9/*Each element is initialised to zero.
10If this were a vector of strings, each
11string would be empty. */
12
13vector<int> vec(4, 42);
14
15/*Creates a vector with 4 elements.
16Each element is initialised to 42. */
17
18
19vector<int> vec(4, 42);
20vector<int> vec2(vec);
21
22/*The second line creates a new vector, copying each element from the
23vec into vec2. */