deque::assign

deque::assign is used to assign new contents to the deque container by replacing its current contents. It changes its size accordingly.

Syntax:

dequename.assign(<int> size, <int> val)

Parameters:

  1. size – Number of values to be assigned to the container.
  2. val – Value to be assigned to the container.

Header File:

<deque>

Exceptions: If an exception is thrown, the container is in a valid state.

Iterator Validity: In this container, all the iterators, pointers, and references are invalidated.

Example:

C++




// C++ program to implement deque::assign
#include <deque>
#include <iostream>
using namespace std;
 
int main()
{
    // Declaration of Deque
    deque<int> d1 = { 1, 3, 6, 0, 0 };
    deque<int> d2;
 
    // Assigning d1 deque elements
    // into d2 deque
    d2.assign(d1.begin(), d1.end());
 
    cout << "Elements in d2 container are : ";
    for (auto it = d2.begin(); it != d2.end(); ++it) {
        cout << *it << " ";
    }
    cout << endl;
    return 0;
}


Output

Elements in d2 container are : 1 3 6 0 0 
  • Time Complexity – O(N)
  • Space Complexity – O(N)

Difference Between deque::assign and deque::empty in C++

Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed.

 Here we will see the difference between deque::assign and deque::at in C++.

Similar Reads

deque::assign

deque::assign is used to assign new contents to the deque container by replacing its current contents. It changes its size accordingly....

deque::empty

...

Differences between deque::assign and deque::empty

deque::empty is used to check whether the deque container is empty or not. It does not change anything in the container....

Contact Us