Open In App

deque assign() function in C++ STL

Last Updated : 02 Nov, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

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:




    // 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;
    }

    
    

    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:




    // 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;
    }

    
    

    Output:

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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads