VECTOR IN C++

  • A vector is a dynamic array whose size can be changed automatically when an element is inserted or deleted.

  • Vector elements are stored in contiguous memory locations so that they can be accessed in constant time.

  • It is defined inside the <vector> header file (also available in <bits/stdc++.h> ).

    The complexity (efficiency) of common operations on vectors is as follows:

  • Random access - constant 𝓞(1).

  • Insertion or removal of elements at the end - amortized constant 𝓞(1).

  • Insertion or removal of elements in front or middle of the vector 𝓞(n).
    image

Ways to declare a vector
Syntax

vector<int> vec; /* Empty */
vector<int> vec( size ); /* Vector with size 'size' */
vector<int> vec( size, value ); /* Vector with size 'size' and all elements with value 'value' */
vector<int> vec = { value1, value2, value3,...,valueN}; /* Vector with N values */

Examples

vector<int> vec; // Declares an empty vector with name as vec ---> []
vector<int> vec(5); // Declares vector of ints with size 5 and the default values assigned are 0s ---> [0,0,0,0,0]
vector<int> vec(5, 10); // Declares vector of ints with size 5 and values assigned are 10s ---> [10,10,10,10,10]
vector<int> vec = {1, 2, 3, 4}; // Declares vector with size 4 and values as ---> [1, 2, 3, 4]
vector<string> vect(5) ; // Declares vector with 5 empty strings ---> [“”, “”, “”, “”, “”, “”]
vector<string> vec(5, “Leetcode”); //Declares vector of strings with size 5 and values initialised are "Leetcode" ---> [“Leetcode”, “Leetcode”, “Leetcode”, “Leetcode”, “Leetcode”]
vector<vector<int>> vec(5);  // Declares vector of vectors ---> [[], [], [], [], []]
vector<vector<int>> vec(5, vector<int>(2)); // Declares vector of vectors(here inside vector size is 2 and default values assigned are 0's) ---> [[0,0], [0,0], [0,0], [0,0], [0,0]]
vector<vector<int>> vec(5, vector<int>(2, 10)); // Declares vector of vectors(here inside vector size is 2 and default values assigned are 10's) ---> [[10,10], [10,10],[10,10],[10,10],[10,10]]

Note : Similar syntax for char long long int float double long double and some other data types include user defined data types.

Ways to insert Elements into vector
Syntax

vec.push_back( ele ); /* adds an element 'ele' at the end and size of vector increases by 1    TC - 𝓞(1)*/
vec.emplace_back( ele ); /* adds an element 'ele' at the end and size of vector increases by 1   TC - 𝓞(1)*/
vec.insert( vec.begin() + i, ele ); /* Inserting element 'ele' at index i | Use i = 0 for inserting at front    TC - 𝓞(n)*/
vec.insert( vec.end(), vec2.begin(), vec2.end() ); /* Inserting the elements of vector 'v2' to the end of vector 'v'    TC - 𝓞(n)*/

Ways to delete elements from vector
Syntax

v.pop_back(); /* Remove element at end     TC - 𝓞(1)*/
v.erase( v.begin() + i ); /* Remove element at index i  or i = 0 to remove first element    TC - 𝓞(n)*/
v.erase( v.begin() + i, v.begin() + j ); /* Remove elements from index i to j-1     TC - 𝓞(n)*/
v.erase( remove( v.begin(), v.end(), value ), v.end() ); /* Removes all elements with value 'value' from the vector      TC - 𝓞(n)*/

Basic inbuilt Functions
Syntax

v.size(); /* returns the size of vector      TC - 𝓞(1)*/
v.resize( size, val ); /* If the 'size' is greater than previous size , then it adds elements with value 'val' at end and if 'size' is less than previous , then it removes from end*/
v.empty(); /* returns true if vector is empty    TC - 𝓞(1)*/
v.front(); or v[0]; /* Accessing first element    TC - 𝓞(1)*/
v.back(); or v[v.size()-1]; /* Accessing last element     TC - 𝓞(1) */
v[i]; /* Accessing element at i'th index (0-based)     TC - 𝓞(1)*/
v.at(i); /* Accessing element at i'th index (0-based)     TC - 𝓞(1)*/
v.clear(); /* Clears vector elements by making vector as empty     TC - 𝓞(n)*/

// Some algorithmic based functions
sort(v.begin(), v.end()); /* sorts vector elements in ascending order by default     TC - 𝓞(nlogn).*/
sort(v.begin(), v.end(), greater<int>); /* sorts vector elements in descending order    TC - 𝓞(nlogn).*/
reverse(v.begin(), v.end()); /* reverses vector elements     TC - 𝓞(n)*/
count(v.begin(), v.end(), x); /* returns the count of x in vector      TC - 𝓞(n)*/
min_element(v.begin(), v.end())-v.begin(); /* returns the minimum element's index(0-based) from the vector     TC - 𝓞(n) */
max_element(v.begin(), v.end())-v.begin();  /* returns the maximum element's index(0-based) from the vector     TC - 𝓞(n) */
*min_element(v.begin(), v.end()); /* returns the minimum element from the vector      TC - 𝓞(n)*/
*max_element(v.begin(), v.end());  /* returns the maximum element from the vector     TC - 𝓞(n) */

Note : The difference between the v[i] and v.at(i) is that 'at' will raise an exception if you try to access an element outside the vector while the [] operator won't.

Lets undertand by sample program

#include <bits/stdc++.h>
using namespace std;

int main() {
	
	vector<int> v = {1, 3, 2};// Create a vector containing integers ---> [1, 3, 2]
	
	// Insertion
	v.push_back(5) ; // Adding 5 at end ---> [1, 3, 2, 5]
	v.emplace_back(4) ; // Adding 4 at end ---> [1, 3, 2, 5, 4]
	v.insert(v.begin(), 10); //inserts 10 at begining ---> [10, 1, 3, 2, 5, 4]
	v.insert(v.begin()+3, 15); //inserts 15 at 3rd index ---> [10, 1, 3, 15, 2, 5, 4]
	
	// Deletion
	v.pop_back(); // removes an element at end ---> [10, 1, 3, 15, 2, 5]
	v.erase(v.begin()); // removes an element at front ---> [1, 3, 15, 2, 5]
	v.erase(v.begin() + 3); // removes an element at 3rd index ---> [1, 3, 15, 5]
	
	// Accessing and Updating
	int a = v[3]; // Here the variable a stores value at 3rd index i.e 5
	v[1] = 100; // Here the value at index 1 is updated with 100 ---> [1, 100, 15, 5]
	int last_element = v.back(); // i.e 5
	int first_element = v.front(); // i.e 1
	
	sort(v.begin(), v.end()); // sorts vector elements ---> [1, 5, 15, 100]
	reverse(v.begin(), v.end()); // reverses vector elements ---> [100, 15, 5, 1]
	
	// Traversing the vector using for loop
	for(int i = 0; i < v.size(); i++){
		cout << v[i] << " "; //After for loop, Output is: 100 15 5 1
	}
	int length = v.size() ; // Here the variable length stores vector size i.e 4
	v.clear() ; //vector is cleared ---> []
	bool isVectorEmpty = v.empty(); //Here the variable isVectorEmpty stores 'true' as the vector is empty
	
	return 0;
}
Comments (1)