deque assign() function in C++ STL

The deque::assign() is a built-in function in C++ STL which is used to assign values to the same or different deque container. On being called more than once in the same program, the function destroys the values of the previous elements and re-assigns new set of elements to the container.

  1. Syntax:
    deque_name.assign(size, val)

    Parameters: The function accepts two parameters which are described below:

    • size: it specifies the number of values to be assigned to the container.
    • val: it specifies the value to be assigned to the container.

    Return Value: The function returns nothing.

    Below programs illustrate the above function:

    Program 1:



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // CPP program to demonstrate the
    // deque::assign() function
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
        deque<int> dq;
      
        // assign 5 values of 10 each
        dq.assign(5, 10);
      
        cout << "The deque elements: ";
        for (auto it = dq.begin(); it != dq.end(); it++)
            cout << *it << " ";
      
        // re-assigns 10 values of 15 each
        dq.assign(10, 15);
      
        cout << "\nThe deque elements: ";
        for (auto it = dq.begin(); it != dq.end(); it++)
            cout << *it << " ";
        return 0;
    }

    chevron_right

    
    

    Output:

    The deque elements: 10 10 10 10 10 
    The deque elements: 15 15 15 15 15 15 15 15 15 15
    
  2. Syntax:
    deque1_name.assign(iterator1, iterator2)

    Parameters: The function accepts two parameters which are described below:

    • iterator1: it specifies the iterator which points to the starting element of container(deque, array, …) whose elements are to be transferred to deque1.
    • iterator2: it specifies the iterator which points to the last element of a container(deque, array, …) whose elements are to be transferred to deque1

    Return Value: The function returns nothing.

    Below programs illustrate the above function:

    Program 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // CPP program to demonstrate the
    // deque::assign() function
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
        deque<int> dq;
      
        // assign 5 values of 10 each
        dq.assign(5, 10);
      
        cout << "The deque elements: ";
        for (auto it = dq.begin(); it != dq.end(); it++)
            cout << *it << " ";
      
        deque<int> dq1;
      
        // assigns all elements from
        // the second position to deque1
        dq1.assign(dq.begin() + 1, dq.end());
      
        cout << "\nThe deque1 elements: ";
        for (auto it = dq1.begin(); it != dq1.end(); it++)
            cout << *it << " ";
        return 0;
    }

    chevron_right

    
    

    Output:

    The deque elements: 10 10 10 10 10 
    The deque1 elements: 10 10 10 10
    


My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

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 :


Be the First to upvote.


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