Skip to content
Related Articles

Related Articles

Common Subtleties in  Vector STLs
  • Difficulty Level : Easy
  • Last Updated : 06 Jul, 2017

Prerequisite – Vector Basics

Following are some important points that can save time on little things in an interview or an important coding contest.

  1. 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]
  2. 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.
    using namespace std;
    int main()
        vector<int> vect;
        for (int i = 0; i < 5; i++)
        // Resizing vector to size 10
        // Prints 0 1 2 3 4 0 0 0 0 0
        for (int i = 0; i < vect.size(); i++)
            cout << vect[i] << " ";
        cout << "\n";
        // Prints 0 1 2 3 4 0 0 0 0 0 50
        for (int i = 0; i < vect.size(); i++)
            cout << vect[i] << " ";
        return 0;
    0 1 2 3 4 0 0 0 0 0
    0 1 2 3 4 0 0 0 0 0 50
  3. clear() function It makes the vector to have zero elements, i.e- no elements and not making the elements to all 0s.
  4. 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++.

  5. 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.
    using namespace std;
    // The vect here is a copy of vect in main()
    void func(vector<int> vect)
    int main()
        vector<int> vect;
        // vect remains unchanged after function
        // call
        for (int i=0; i<vect.size(); i++)
           cout << vect[i] << " ";
        return 0;

    Output :

    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 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

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :