Difference Between deque::assign and deque::back 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.
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:
- 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: 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
Basis | deque::assign | deque::back |
---|---|---|
Definition | It 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. |
Syntax | dequename.assign(<int> size, <int> val); | dequename.back() |
No of Parameters | It takes two parameters | It does not take any parameters. |
Return Value | It does not have any return value. | Its return value is of Integer type |
Complexity | Linear | Constant |
Contact Us