1// CPP program to create an empty vector
2// and push values one by one.
3#include <vector>
4
5using namespace std;
6int main()
7{
8 // Create an empty vector
9 vector<int> vect;
10 //add/push an integer to the end of the vector
11 vect.push_back(10);
12 //to traverse and print the vector from start to finish
13 for (int x : vect)
14 cout << x << " ";
15
16 return 0;
17}
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. */
1vector<int> g1;
2
3 for (int i = 1; i <= 5; i++)
4 g1.push_back(i);
5
6 cout << "Output of begin and end: ";
7 for (auto i = g1.begin(); i != g1.end(); ++i)
8 cout << *i << " ";
9
10 cout << "\nOutput of cbegin and cend: ";
11 for (auto i = g1.cbegin(); i != g1.cend(); ++i)
12 cout << *i << " ";
13
14 cout << "\nOutput of rbegin and rend: ";
15 for (auto ir = g1.rbegin(); ir != g1.rend(); ++ir)
16 cout << *ir << " ";
17