Open In App

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

Last Updated : 26 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Deque or Double-ended queue 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++.

deque::begin

deque::begin is used to return an iterator pointing to the first element in the deque container. It returns a random access iterator pointing to it.

Syntax:

 iterator begin();

Parameters: None

Return Value: Its return value is an iterator to the beginning of the sequence container.

Iterator Validity: No change in Iterator Validity

Header File:

<deque>

Exceptions: deque::begin never throws an exception

Example:

C++




// C++ program to implement deque::begin
  
#include <deque>
#include <iostream>
using namespace std;
  
// Driver code
int main()
{
    // Declaration of Deque
    deque<int> GFG = { 1, 2, 3, 4, 5 };
  
    // Iterator Pointing to the first element in the
    // container
    deque<int>::iterator itr = GFG.begin();
  
    cout << "Elements in Deque are : ";
  
    for (itr = GFG.begin(); itr != GFG.end(); ++itr) {
        cout << *itr << " ";
    }
  
    cout << endl;
    return 0;
}


Output:

Elements in Deque are : 1 2 3 4 5 
  • Time Complexity: O(N)
  • Space Complexity: O(1)

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>

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

Example:

C++




// C++ program to implement deque::assign
  
#include <deque>
#include <iostream>
using namespace std;
  
// Driver codea
int main()
{
    // Declaration of Deque
    deque<int> A = { 5, 10, 15, 20 };
    deque<int> B;
  
    // Iterator for deque B to traverse
    deque<int>::iterator itr1 = B.begin();
  
    // Iterator for deque A to traverse
    deque<int>::iterator itr2 = A.begin();
  
    // Assigning deque A elements
    // into deque B
    B.assign(A.begin(), A.end());
  
    cout << "Elements in Container A are : ";
    for (itr2 = A.begin(); itr2 != A.end(); ++itr2) {
        cout << *itr2 << " ";
    }
    cout << endl;
  
    cout << "Elements in Container B are : ";
    for (itr1 = B.begin(); itr1 != B.end(); ++itr1) {
        cout << *itr1 << " ";
    }
  
    cout << endl;
    return 0;
}


Output:

Elements in Container A are : 5 10 15 20 
Elements in Container B are : 5 10 15 20 
  • Time Complexity: O(N)
  • Space Complexity: O(N)

deque::assign vs deque::begin

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

Basis deque::begin deque::assign
Definition It is used to return an iterator pointing to the first element in the deque container. It is used to return new contents to the deque container, replacing its current contents
Syntax iterator begin(); dequename.assign(<int> size, <int> val);
No of Parameters It does not take any parameters It takes two parameters.
Return Value Its return value is an iterator to the beginning of the sequence container. It does not have any return type
Complexity Constant Linear


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

Similar Reads