1 /*Member functions*/
2 Iterators
3 -----------------------------------------
4 begin | Returns an iterator to the beginning
5 end | Returns an iterator to the end
6
7 Capacity
8 -----------------------------------------
9 empty | Checks whether the container is empty
10 size | Returns the number of elements
11 reserve | Reserves storage
12 capacity | Returns the number of elements that can be held in currently allocated storage
13
14 Element access
15 -----------------------------------------
16 at | Access specified element with bounds checking
17 front | Access the first element
18 back | Access the last element
19 operator[] | Access specified element
20
21 Modifiers
22 -----------------------------------------
23 clear | Clears the contents
24 insert | Inserts elements
25 emplace | Constructs element in-place
26 erase | Erases elements
27 push_back | Adds an element to the end
28 emplace_back | Constructs an element in-place at the end
29 pop_back | Removes the last element
30 resize | Changes the number of elements stored
31 swap | Swaps the contents
32
33 *Notes*
34 - https://en.cppreference.com/w/cpp/container/vector
35 - https://www.geeksforgeeks.org/vector-in-cpp-stl/
36 - https://www.tutorialspoint.com/cpp_standard_library/vector.htm
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
1#include <iostream>
2#include <vector>
3
4class Object
5{
6 public:
7 Object()
8 {}
9 ~Object()
10 {}
11 void AddInt(int num)
12 {
13 m_VectorOfInts.push_back(num);
14 }
15 std::vector<int> GetCopyOfVector()
16 {
17 return m_VectorOfInts;
18 }
19 void DisplayVectorContents()
20 {
21 for( unsigned int i = 0; i < m_VectorOfInts.size(); i++ )
22 {
23 std::cout << "Element[" << i << "] = " << m_VectorOfInts[i] << std::endl;
24 }
25 std::cout << std::endl;
26 }
27
28 private:
29 std::vector<int> m_VectorOfInts;
30};
31
32int main()
33{
34 // Create our class an add a few ints
35 Object obj;
36 obj.AddInt(32);
37 obj.AddInt(56);
38 obj.AddInt(21);
39
40 // Display the vector contents so far
41 obj.DisplayVectorContents();
42
43 // Creates a copy of the classes container you can only really view whats in
44 // the classes vector container. What ever changes you make here wont effect the class.
45 std::vector<int> container1 = obj.GetCopyOfVector();
46 // These elements wont be added as it's a copy of the container
47 container1.push_back(342);
48 container1.push_back(64);
49 container1.push_back(123);
50
51
52 // Display the classes container to see show nothing was added.
53 obj.DisplayVectorContents();
54
55 return 0;
56}