Skip to content
Related Articles

Related Articles

Improve Article

Modifiers for Vector in C++ STL

  • Difficulty Level : Medium
  • Last Updated : 28 Jun, 2021

Click here for Set 1 of Vectors.

 

Modifiers
1.1 assign(input_iterator first, input_iterator last) – Assigns new content to vector and resize
1.2 assign(size_type n, const value_type g) – Assigns new content to vector and resize




#include <iostream>
#include <vector>
  
using namespace std;
  
int main()
{
    vector <int> g1;
    vector <int> g2;
    vector <int> g3;
  
    g1.assign(5, 10);   // 5 elements with value 10 each
  
    vector <int> :: iterator it;
    it = g1.begin() + 1;
  
    g2.assign(it, g1.end() - 1); // the 3 middle values of g1
  
    int gquiz[] = {1, 2};
    g3.assign(gquiz, gquiz + 2);   // assigning from array
  
    cout << "Size of g1: " << int(g1.size()) << '\n';
    cout << "Size of g2: " << int(g2.size()) << '\n';
    cout << "Size of g3: " << int(g3.size()) << '\n';
    return 0;
}

The output of the above program is :

Size of g1: 5
Size of g2: 3
Size of g3: 2

2. push_back(const value_type g) – Adds a new element ‘g’ at the end of the vector and increases the vector container size by 1
3. pop_back() – Removes the element at the end of the vector, i.e., the last element and decreases the vector container size by 1






#include <iostream>
#include <vector>
  
using namespace std;
  
int main()
{
  vector <int> gquiz;
  int sum = 0;
  gquiz.push_back(10);
  gquiz.push_back(20);
  gquiz.push_back(30);
  
  while (!gquiz.empty())
  {
    sum += gquiz.back();
    gquiz.pop_back();
  }
  
  cout << "The sum of the elements of gquiz is :  "
       << sum << '\n';
  
  return 0;
}

The output of the above program is :

The sum of the elements of gquiz is : 60

4.1 insert(const_iterator q, const value_type g) – Adds element ‘g’ before the element referenced by iterator ‘q’ and returns an iterator that points to the newly added element
4.2insert(const_iterator q, size_type n, const value_type g) – Adds ‘n’ elements each with value ‘g’ before the element currently referenced by iterator ‘q’ and returns an iterator that points to the first of the newly added elements
4.3 insert(const_iterator q, InputIterator first, InputIterator last) – Adds a range of elements starting from first to last, the elements being inserted before the position currently referred by ‘q’




#include <iostream>
#include <vector>
  
using namespace std;
  
int main()
{
    vector <int> gquiz1(3, 10);
    vector <int> :: iterator it;
  
    it = gquiz1.begin();
    it = gquiz1.insert(it, 20);
  
    gquiz1.insert(it, 2, 30);
  
    it = gquiz1.begin();
  
    vector <int> gquiz2(2, 40);
    gquiz1.insert(it + 2, gquiz2.begin(), gquiz2.end());
  
    int gq [] = {50, 60, 70};
    gquiz1.insert(gquiz1.begin(), gq, gq + 3);
  
    cout << "gquiz1 contains : ";
    for (it = gquiz1.begin(); it < gquiz1.end(); it++)
        cout << *it << '\t';
  
    return 0;
}

The output of the above program is :

gquiz1 contains : 50    60    70    30    30    
40    40    20    10    10    10

5.1 erase(const_iterator q) – Deletes the element referred by ‘q’ and returns an iterator to the element followed by the deleted element
5.2 erase(const_iterator first, const_iterator last) – Deletes the elements in the range first to last, with the first iterator included in the range and the last iterator not included, and returns an iterator to the element followed by the last deleted element




#include <iostream>
#include <vector>
using namespace std;
  
int main ()
{
    vector <int> gquiz;
  
    for (int i = 1; i <= 10; i++)
        gquiz.push_back(i * 2);
  
    // erase the 5th element
    gquiz.erase(gquiz.begin() + 4);
  
    // erase the first 5 elements:
    gquiz.erase(gquiz.begin(), gquiz.begin() + 5);
  
    cout << "gquiz contains :";
    for (int i = 0; i < gquiz.size(); ++i)
        cout << gquiz[i] << '\t';
  
    return 0;
}

The output of the above program is :

gquiz contains :14    16    18    20

6. swap(vector q, vector r) – Swaps the contents of ‘q’ and ‘r’
7. clear() – Removes all elements from the vector




#include <iostream>
#include <vector>
  
using namespace std;
  
int main()
{
    vector <int> gquiz1;
    vector <int> gquiz2;
    vector <int> :: iterator i;
  
    gquiz1.push_back(10);
    gquiz1.push_back(20);
  
    gquiz2.push_back(30);
    gquiz2.push_back(40);
  
    cout << "Before Swapping, \n"
         <<"Contents of vector gquiz1 : ";
  
    for (i = gquiz1.begin(); i != gquiz1.end(); ++i)
        cout << *i << '\t';
  
    cout << "\nContents of vector gquiz2 : ";
    for (i = gquiz2.begin(); i != gquiz2.end(); ++i)
        cout << *i << '\t';
  
    swap(gquiz1, gquiz2);
  
    cout << "\n\nAfter Swapping, \n";
    cout << "Contents of vector gquiz1 : ";
    for (i = gquiz1.begin(); i != gquiz1.end(); ++i)
        cout << *i << '\t';
  
    cout << "\nContents of vector gquiz2 : ";
    for (i = gquiz2.begin(); i != gquiz2.end(); ++i)
        cout << *i << '\t';
  
    cout << "\n\nNow, we clear() and then add "
         << "an element 1000 to vector gquiz1 : ";
    gquiz1.clear();
    gquiz1.push_back(1000);
    cout << gquiz1.front();
  
    return 0;
}

The output of the above program is :

Before Swapping, 
Contents of vector gquiz1 : 10    20    
Contents of vector gquiz2 : 30    40    

After Swapping, 
Contents of vector gquiz1 : 30    40    
Contents of vector gquiz2 : 10    20    

Now, we clear() and then add an element 1000 to vector gquiz1 : 1000

 
Click here for Set 3 of Vectors.
 
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 :