Prerequisite – Vector Basics
Following are some important points that can save time on little things in an interview or an important coding contest.
- vector <int> vect(10) vs vector<int> vect[10]
// Creates a vector vect[] of size 10 vector <int> vect(10) // creates an array of vectors vect[] of size // 10 where each vector has int members vector<int> vect[10]
- resize() and push_back():
After the resize() function has been used on a vector, if push_back() is used on the same vector, the elements being pushed back get added at the end of the resized vector, and not into it.// A C++ program to demonstrate that push_back()// happens at the end of resized vector.#include<bits/stdc++.h>usingnamespacestd;intmain(){vector<int> vect;for(inti = 0; i < 5; i++)vect.push_back(i);// Resizing vector to size 10vect.resize(10);// Prints 0 1 2 3 4 0 0 0 0 0for(inti = 0; i < vect.size(); i++)cout << vect[i] <<" ";cout <<"\n";vect.push_back(50);// Prints 0 1 2 3 4 0 0 0 0 0 50for(inti = 0; i < vect.size(); i++)cout << vect[i] <<" ";return0;}chevron_rightfilter_noneOutput: 0 1 2 3 4 0 0 0 0 0 0 1 2 3 4 0 0 0 0 0 50
- clear() function It makes the vector to have zero elements, i.e- no elements and not making the elements to all 0s.
- Creating a two dimensional vector
// This doesn't work vector<vector<int>> vect; // This works fine vector< vector <int> > vect;
The difference between these two statements is that the first statement has a space between the angular brackets ( > >). Writing without the space doesn’t work because >> is an operator in C++.
- Passing vectors to functions:
When a vector is simply passed to a function, a copy of the vector is created. This might take a lot of time in cases of large vectors.// C++ program to demonstrate that when vectors// are passed to functions without &, a copy is// created.#include<bits/stdc++.h>usingnamespacestd;// The vect here is a copy of vect in main()voidfunc(vector<int> vect){vect.push_back(30);}intmain(){vector<int> vect;vect.push_back(10);vect.push_back(20);func(vect);// vect remains unchanged after function// callfor(inti=0; i<vect.size(); i++)cout << vect[i] <<" ";return0;}chevron_rightfilter_noneOutput :
10 20
In situations where we donât actually need to have a copy of the vector, the declaration should be made as follows:
// It is recommended to pass vectors by reference // wherever possible. int func(vector<int>& vect) { }
This article is contributed by Supiya Shrivatsa. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- vector::front() and vector::back() in C++ STL
- vector::empty() and vector::size() in C++ STL
- vector::push_back() and vector::pop_back() in C++ STL
- vector::operator= and vector::operator[ ] in C++ STL
- vector::at() and vector::swap() in C++ STL
- vector::crend() & vector::crbegin() with example
- vector::begin() and vector::end() in C++ STL
- vector :: cbegin() and vector :: cend() in C++ STL
- How to flatten a Vector of Vectors or 2D Vector in C++
- How to find common elements between two Vector using STL in C++?
- Sorting 2D Vector in C++ | Set 2 (In descending order by row and column)
- Sorting 2D Vector in C++ | Set 1 (By row and column)
- Sorting Vector of Pairs in C++ | Set 1 (Sort by first and second)
- Sorting Vector of Pairs in C++ | Set 2 (Sort in descending order by first and second)
- Sorting 2D Vector in C++ | Set 3 (By number of columns)
- Ways to copy a vector in C++
- Convert an array to reduced form | Set 2 (Using vector of pairs)
- Vector in C++ STL
- Modifiers for Vector in C++ STL
- std::upper_bound and std::lower_bound for Vector in C++ STL

