Difference Between deque::assign and deque::at 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++.
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.
In this container, all the iterators, pointers, and references are invalidated. The header file for using deque::assign is <deque>. If an exception is thrown, the container is in a valid state.
Below is the C++ program to implement deque::assign:
C++
// C++ program to implement deque::assign #include <iostream> #include<deque> using namespace std; // Driver code int main() { // Declaration of Deque deque< int > first = {10, 20, 30, 40, 50, 60}; deque< int > second; // Iterator for deque to traverse deque< int >::iterator itr = second.begin(); // Assigning first deque elements // into second second.assign(first.begin(), first.end()); cout << "Elements after assigning : " ; for (itr = second.begin(); itr != second.end(); ++itr) { cout <<*itr<< " " ; } cout << endl; return 0; } |
Elements after assigning : 10 20 30 40 50 60
- Time complexity: O(N).
- Space Complexity: O(N).
Deque::at
deque::at is used to return a reference to the element at position x in the deque container object. Deque::at automatically checks whether x is within the bounds of valid elements in the container or not.
Syntax:
at (size_type n);
Parameters:
Position of the element to be fetched.
Return Value: Direct reference to the element at the given position.
In this container all the iterators are valid. The header file for using deque::at is <deque>.
Exception:
- If an exception is thrown, there are no changes in the container.
- It throws out_of_range if n is out of bounds.
Below is the C++ program to implement deque::at:
C++
// C++ program to implement // deque::at #include <iostream> #include <deque> using namespace std; // Driver code int main () { // 10 zero-initialized unsigneds deque< int > gfg (10); // Here we have assigned some values for ( int i = 0; i < gfg.size(); i++) gfg.at(i) = i; cout << "Elements after using deque::at -:" ; for ( int i = 0; i < gfg.size(); i++) { cout << ' ' << gfg.at(i); } cout << endl; return 0; } |
Elements after using deque::at -: 0 1 2 3 4 5 6 7 8 9
- Time Complexity: O(1)
- Space Complexity: O(1)
deque::assign vs deque::at
Below are the differences between deque::assign and deque::at:
Basis | deque::assign | deque::at |
---|---|---|
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 element at position n in the deque container object. |
Syntax | dequename.assign(<int> size, <int> val); | reference at (size_type n); |
No. Of Parameters | It takes two parameters. | It takes only one parameter. |
Return Value | It does not have any return type. | Returns a direct reference to the element at the given position. |
Complexity | Its complexity is linear. | Its complexity is constant. |