Difference Between deque::cbegin and deque::assign in C++
Last Updated :
26 Jul, 2022
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::cbegin
deque::cbegin is used to return a const iterator pointing to the first element in the container. We cannot use this to modify the contents the iterator points to.
Syntax:
const_iterator cbegin();
Parameters: None
Return Value: A const_iterator to the beginning of the sequence.
Iterator Validity: There is no change in Iterator Validity
Header File:
<deque>
Exceptions: It never throws exceptions
Example:
C++
#include <deque>
#include <iostream>
using namespace std;
int main()
{
deque< int > GFG = { 1, 2, 3, 4, 5 };
cout << "Elements in Deque Container are : " ;
for ( auto it = GFG.cbegin(); it != GFG.cend(); ++it) {
cout << *it << " " ;
}
cout << endl;
return 0;
}
|
Output:
Elements in Deque Container are : 1 2 3 4 5
- Time Complexity – O(1)
- 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:
- 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>
Exceptions: If an exception is thrown, the container is in a valid state.
Example:
C++
#include <deque>
#include <iostream>
using namespace std;
int main()
{
deque< int > A = { 10, 20, 30, 40, 50, 60 };
deque< int > B;
deque< int >::iterator itr = B.begin();
B.assign(A.begin(), A.end());
cout << "Elements in Deque B are : " ;
for (itr = B.begin(); itr != B.end(); ++itr) {
cout << *itr << " " ;
}
cout << endl;
return 0;
}
|
Output:
Elements in Deque B are : 10 20 30 40 50 60
- Time Complexity: O(N)
- Space Complexity: O(N)
deque::assign vs deque::cbegin
Below are the differences between deque::assign and deque::cbegin
Basis |
deque::cbegin |
deque::assign |
Definition |
It is used to return a const iterator pointing to the first element in the container. |
It is used to assign new contents to the deque container, replacing its current contents |
Syntax |
const_iterator cbegin(); |
dequename.assign(<int> size, <int> val); |
Parameters |
It does not take any parameters. |
It takes two parameters i.e. size and val |
Return Value |
Its return value is a const_iterator to the beginning of the sequence. |
It does not have any return type. |
Complexity |
Constant |
Linear |
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...