Vector Class in STL in C++

Vector Class in STL in C++

The most general purpose container in C++ is Vector. It supports a dynamic array.  Array is fixed in size and there is no flexibility to grow or shrink array size during execution but Vectors provide memory flexibility.

What are Vectors in C++?

1. Vector is a part of STL (Standard Template Library).

2. Vector supports a dynamic array which can be changed during runtime according to the user's need.

3. Vectors in C++ are sequence containers which represents array that can change their size during runtime.

4. Vector uses contiguous storage locations for the elements which are to be stored in it.

5. It is the most general purpose container in STL. 

6. Array size is fixed, no flexibility to grow or shrink during execution (Array is static in nature). However, Vector provides us memory flexibility. 

7. Vector being a dynamic array, doesn't need size during declaration. 

8. Vector can adjust its size automatically when an element is inserted or deleted from it.

9. Vector is not ordered in C++.

10. It is necessary to include #include<vector> library in order to use vectors in C++.

11. We can also create vector of vectors in C++. Vector of vectors is a 2-dimensional vector with a variable number of rows where each row is considered as a vector.

12. In Vector of vectors, each index of vector stores a vector inside it. It can be accessed or traversed using iterators.

Syntax of Vector in C++

vector <object_type> Vector_Name;
It will create a blank vector.

Example:
vector <string> VECTOR1 {"Learning", "Mania"};


Syntax of Vector of Vectors in C++

vector<vector<data_type>> Vector_Name;

Example:
vector<vector<int>> VECTOR1;


More Vector Declaration

vector <int> Vector1;              //It is Zero length vector.
vector <char> Vector2(5);          //It creates a 5 element char vector.
vector <char> Vector3(9, 'V');     //It initializes 9 elements char vector with 'V'.

Relational Operators

1. ==  is equal to
2. !=   is not equal to
3. >    greater than
4. <    less than
5. >=  greater than or equals to
6. <=  less than or equals to

Various Functions in Vectors

1. Subscript operator [ ] - Subscript operator is also defined for vector.

2. size() - It returns the number of elements that are present currently in the vector.

3. max_size() - It returns the maximum number of elements that a vector can hold.

4. capacity() - It returns the capacity of the vector. This is the number of elements it can hold before it will need to allocate more memory.

5. resize(m) - It resized the container to store 'm' elements in the vector.

6. empty() - It returns whether the container (vector) is empty or not.

7. reference_operator[n] - It returns a reference to the 'n' element in the vector.

8. data() - It returns a direct pointer to the memory array which is used internally by the vector to store its owned elements.

9. at(i) - It works same in case of vector as it works for array. It returns the element at ith index in the vector.

10. front() - It returns the 1st element of the vector.

11. back() - It returns the last element of the vector.

12. begin() - It returns an iterator pointing to the first element in the vector.

13. end() - It returns an iterator pointing to the last element in the vector.

14. rbegin() - It returns a reverse iterator pointing to the last element in the vector.

15. rend() - It returns a reverse iterator pointing to the element preceding the first element in the vector. It is basically considered as a reverse end.

16. cbegin() - It returns a constant iterator pointing to the first element in the vector.

17. cend() - It returns a constant iterator pointing to the element that follows the last element in the vector.

18. crbegin() - It returns a constant reverse iterator pointing to the last element in the vector.

19. crend() - It returns a constant reverse iterator pointing to the element preceding the first element in the vector.

20. assign() - It assigns a new value to the existing elements of the vector.

21. push_back() - It is a member function, which can be used to add value to the vector at the end.

22. pop_back() - It removes the last element from the vector.

23. insert() - It inserts an element before a specified element in the vector.

24. erase() - It is used to remove elements from a specified element or a range in the vector.

25. clear() - It removes all the elements that are present inside the vector.

26. swap() - It is used to swap the contents of two vectors of the same datatypes. The size of the vectors may differ.

Allocators Function in Vector in C++

1. An allocator is an object which dynamically allocates memory and dynamically deallocates memory.

2. In C++, only one function can be used as an allocator in vector which is get_allocator() function.

3. get_allocator() is used to allocate a chunk of memory.


Example of Vectors in C++
#include<iostream>
#include<vector>
using namespace std;

int main() {
 vector <int> Vector1;
 vector <int> Vector2 {1, 2, 3};
 vector <int> Vector3 (4);
 vector <int> Vector4 (5,10);
 
 cout << Vector4[4];                               //10
 cout << Vector1.capacity();                        //0
 
 Vector1.push_back(5);
 cout << Vector1.capacity();                        //1
 
 Vector1.push_back(9);
 cout << Vector1.capacity();                        //2
 
 Vector1.push_back(11);
 cout << Vector1.capacity();                        //4
 
 cout << Vector1.size();                            //3
 
 for (int i=0; i<Vector2.size();i++) {
  cout << Vector2[i];
 }                                                 //1 2 3
 
 Vector1.clear();
 cout << Vector1.capacity();                       //4
 cout << Vector1.size();                           //0
 
 cout << Vector4.at(2);                            //10
 cout << Vector2.front();                          //1
 
 cout << Vector2.back();                           //3
 
 vector <int> :: iterator it = Vector1.begin();
 Vector4.insert(it+3, 15);
 
 for (int i=0; i<Vector4.size(); i++) {
  cout << Vector4[i];
 }                                                 //10 10 10 15 10 10
}


Previous
Next Post »