In the C++ programming language, the deque::crend function is present in the deque container that is part of the Standard Template Library (STL). It is commonly used to return an iterator pointing to the element preceding the first element of the deque container. An iterator can be incremented or decremented, but it cannot modify the content of the deque. The iterator that the method returns is a constant reverse iterator.
Syntax
It has the following syntax:
const_reverse_iterator crend();
dequeName.crend( );
In this syntax,
- Parameter: It does not contain any parameter.
- Return value: It returns a constant reverse iterator referring to the element preceding the first element of the deque container.
Now, let us look at some of the examples to understand the practical usage of the deque crend method.
C++ deque crend Function Example
Let us consider a simple example to demonstrate the deque crend function, where we have created a 'char' type deque in C++.
Example
#include <iostream>
#include<deque>
using namespace std; //using standard namespace
int main() //main function
{
deque<char> c={'l','a','p','t','o','p'};
deque<char>::const_reverse_iterator citr=c.crbegin();
cout<<"Reverse deque: ";
while(citr!=c.crend())
{
cout<<*citr;
++citr;
}
return 0;
}
Output:
Reverse deque: potpal
Explanation:
In this example, we have created a deque of 'char' type that contains some elements inside it. After that, we have declared a constant reverse iterator that we have executed from the beginning of the deque using the 'crbegin' method. Next, we use the while loop, which iterates till the iterator 'citr' does not become equal to the 'c.crend' function. In the loop, we have displayed the iterator and pre-incremented the iterator by 1. In the output, we have got the character elements displayed from the end to the beginning, i.e., 'laptop' converted to 'potpal'.
Let us take a simple example to demonstrate the crbegin and crend functions, where we have created an 'int' type deque in C++.
Example
#include <iostream>
#include <deque>
using namespace std; //using standard namespace
int main() //main function
{
deque <int> val = { 42, 34, 56, 90, 12 };
deque <int>::const_reverse_iterator citr = val.crbegin( );
cout <<"After performing the reverse deque, we get : " << endl ;
while( citr != val.crend( ) )
{
cout << *citr << endl;
++citr;
}
return 0;
}
Output:
After performing the reverse deque, we get :
12
90
56
34
42
Explanation:
In this example, we have created a deque of integer type that contains several integer numbers. After that, we have declared a constant reverse iterator that we have executed from the beginning of the deque using the 'crbegin' method. Next, we use the while loop that iterates till the iterator 'citr' does not become equal to the 'val.crend' function. In the loop, we have displayed the iterator and pre-incremented the iterator by 1. In the output, we have the integer elements displayed from the end to the beginning.
C++ crend function Example with a String Deque
Let us take an example to demonstrate the crend function with a string deque in C++.
Example
#include <iostream>
#include <deque>
#include <string>
using namespace std; //using standard namespace
int main() { //main function
deque<string> name = {"Alexa", "Peter", "Johnson", "Robert"};
cout << "Names in the reverse order (read-only traversal):\n";
for (auto it = name.crbegin(); it != name.crend(); ++it) {
cout << *it << endl;
}
return 0;
}
Output:
Names in the reverse order (read-only traversal):
Robert
Johnson
Peter
Alexa
Explanation:
In this example, we create a deque of strings named name that contains four elements. After that, we use a loop that uses the crbegin and crend functions to traverse the deque in reverse order, which displays elements from the last to the first (in reverse order). As both functions are constant reverse iterators, the elements can only be accessed (read) and not changed during traversal.
Features of the Deque crend function in C++
There are several features of the deque crend function in C++. Some of them are as follows:
- The deque crend function in C++ is commonly utilized to return a constant reverse iterator thacpp tutorials to the position before the first element of the deque container.
- In the C++ programming language, we cannot modify the elements if we access the elements using the deque crend function. It helps to ensure the correctness and data safety of the program.
- We can traverse the elements using the deque crend function because it returns a const iterator. However, we cannot change their values.
- The deque crend function in C++ is generally utilized with the crbegin function to perform the reverse traversal operation of the deque from end to beginning.
- If we want to perform any operation using the deque crend function in C++, it takes only constant time complexity, i.e., O(1).
- In C++, the deque crend function doesn't take any argument. It just returns an iterator object.
Conclusion
In conclusion, the deque::crend function in C++ is a simple and efficient feature of the Standard Template Library (STL) that enables read-only reverse traversal of a deque container. It is commonly utilized to return a constant reverse iterator thacpp tutorials to the position before the first element. It ensures that the elements cannot be modified while iterating.
In C++, it is generally utilized with the crbegin function. It gives an efficient and secure method to access deque elements in reverse order, which helps to maintain the const-correctness and data integrity.
C++ Deque crend Function FAQs
1) What is the main purpose of the deque crend function in C++?
The deque crend function in C++ is an in-built function of the deque container. It is commonly utilized to return a constant reverse iterator thacpp tutorials to the position before the first element, which ensures that the elements cannot be modified while iterating.
2) What is the key difference between the rend and crend functions in C++?
In the C++ programming language, the key difference between the rend function and the crend function is that the rend function is commonly utilized to return a reverseiterator that enables modifications. In contrast, the crend function is commonly utilized to return a constreverse_iterator that enables read-only access.
3) Can the crend function be utilized with the non-const deques in C++?
Yes, the crend function in C++ can be utilized with both const and non-const deque objects. It allows read-only access to the elements in both cases.
4) Can we modify elements using the iterator returned by the crend function in C++?
No, we cannot modify the elements because the crend function returns a constant reverse iterator in C++. It allows only accessibility to read-only elements.
5) What is the time complexity of the crend function in C++?
In C++, if we want to perform any operation using the deque crend function, it takes only constant time complexity, i.e., O(1).