vector :: assign() in C++ STL

vector:: assign() is an STL in C++ which assigns new values to the vector elements by replacing old ones. It can also modify the size of the vector if necessary.

Syntax for assigning constant values:

vectorname.assign(int size, int value)

Parameters: 
  • size - number of values to be assigned
  • value - value to be assigned to the vectorname
  • Program 1: The program below shows how to assign constant values to a vector



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // CPP program to demonstrate
    // how to assign constant values to a vector
      
    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
        vector<int> v;
        v.assign(7, 100);
      
        cout << "Size of first: " << int(v.size()) << '\n';
      
        cout << "Elements are\n";
        for (int i = 0; i < v.size(); i++)
            cout << v[i] << endl;
        return 0;
    }

    chevron_right

    
    

    Output:

    Size of first: 7
    Elements are
    100
    100
    100
    100
    100
    100
    100
    

    Syntax for assigning values from an array or list:

    vectorname.assign(arr, arr + size)
    
    Parameters: 
    
  • arr - the array which is to be assigned to a vector
  • size - number of elements from the beginning which has to be assigned.
  • Program 2: The program below shows how to assign values from an array or list

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // CPP program to demonstrate
    // how to assign values to a vector
    // from a list
      
    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
        vector<int> v1;
        int a[] = { 1, 2, 3 };
      
        // assign first 2 values
        v1.assign(a, a + 2);
      
        cout << "Elements of vector1 are\n";
        for (int i = 0; i < v1.size(); i++)
            cout << v1[i] << " ";
      
        vector<int> v2;
        // assign first 3 values
        v2.assign(a, a + 3);
      
        cout << "\nElements of vector2 are\n";
        for (int i = 0; i < v2.size(); i++)
            cout << v2[i] << " ";
      
        return 0;
    }

    chevron_right

    
    

    Output:

    Elements of vector1 are
    1 2 
    Elements of vector2 are
    1 2 3 
    

    Syntax for modifying values from a vector

    vectorname.assign(InputIterator first, InputIterator last) 
    
    Parameters: 
    
  • first - Input iterator to the initial position range.
  • last - Input iterator to the final position range.
  • Program 3: The program below shows how to modify the vector

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // CPP program to demonstrate
    // how to modify vector size
      
    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
        vector<int> v;
        v.assign(7, 100);
      
        cout << "Size of first: " << int(v.size()) << '\n';
      
        cout << "Elements are\n";
        for (int i = 0; i < v.size(); i++)
            cout << v[i] << endl;
      
        // modify the elements
        v.assign(v.begin(), v.begin() + 3);
      
        cout << "\nModified VectorElements are\n";
        for (int i = 0; i < v.size(); i++)
            cout << v[i] << endl;
        return 0;
    }

    chevron_right

    
    

    Output:

    Size of first: 7
    Elements are
    100
    100
    100
    100
    100
    100
    100
    
    Modified VectorElements are
    100
    100
    100
    


    My Personal Notes arrow_drop_up

    Striver(underscore)79 at Codechef and codeforces D

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




    Article Tags :
    Practice Tags :


    3


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.