Skip to content
Related Articles

Related Articles

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

View Discussion
Improve Article
Save Article
  • Last Updated : 26 Jul, 2022
View Discussion
Improve Article
Save Article

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.

Deque::assign

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

Syntax:

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

Parameters:

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

Return Value: None

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

Header File:

<deque>

Example :

C++




// C++ program to demonstrate deque::assign
#include <deque>
#include <iostream>
using namespace std;
  
// Driver code
int main()
{
    // Declaration of Deque
    deque<int> one = { 7, 14, 21, 28, 35 };
    deque<int> two;
  
    // Iterator for deque to traverse
    deque<int>::iterator itr = two.begin();
  
    // Assigning  deque<int>one elements
    // into deque<int>two
    two.assign(one.begin(), one.end());
  
    cout << "Elements of deque<int>one are : ";
    for (itr = one.begin(); itr != one.end(); ++itr) {
        cout << *itr << " ";
    }
    cout << endl;
    cout << "Elements of deque<int>two are : ";
    for (itr = two.begin(); itr != two.end(); ++itr) {
        cout << *itr << " ";
    }
  
    cout << endl;
    return 0;
}

Output:

Elements of deque<int>one are : 7 14 21 28 35 
Elements of deque<int>two are : 7 14 21 28 35 

Time Complexity: O(N)

Space Complexity: O(N)

Deque::back

deque::back is used to return a reference to the last element in the container. It returns the direct reference of the element.

Syntax:

deque_name.back()

Parameters: It does not take any parameters

Return Value: Deque::back accepts an Integer type return value

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

Header File:

<deque>

Exceptions: If the container is empty then it will cause undefined behavior.

Example:

C++




// C++ program to demonstrate deque::back
  
#include <deque>
#include <iostream>
using namespace std;
  
// Driver code
int main()
{
    // Declaration of Deque
    deque<int> GFG = { 10, 20, 30, 40, 50, 60 };
  
    int last_element = GFG.back();
    cout << "Last ELement is : " << last_element << endl;
    return 0;
}

Output -:

Last ELement is : 60

Time Complexity: O(1)

Space Complexity: O(1)

deque::assign vs deque::back

Below are the differences between deque::assign and deque::back

Basisdeque::assigndeque::back
DefinitionIt is used to assign new contents to the deque container, replacing its current contents.It is used to return a reference to the last element in the container.
Syntaxdequename.assign(<int> size, <int> val);dequename.back()
No of ParametersIt takes two parametersIt does not take any parameters.
Return ValueIt does not have any return value.Its return value is of Integer type
ComplexityLinearConstant

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!