In C++, the deque::crend method belongs to the deque container within the Standard Template Library (STL). It is frequently employed to provide an iterator that points to the element just before the initial element of the deque container. While the iterator can be adjusted in position, it is not capable of altering the deque's content. The iterator yielded by this function is a constant reverse iterator.
Syntax
It has the following syntax:
const_reverse_iterator crend();
dequeName.crend( );
In this particular format,
- Argument: It does not include any argument.
- Output: It yields a constant reverse iterator pointing to the element before the initial element of the deque container.
Now, let's explore a few examples to grasp how the deque crend method is practically utilized.
C++ deque crend Function Example
Let's explore a basic illustration to showcase the deque crbegin operation, involving the establishment of a deque of 'char' type 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 illustration, a deque of 'char' type has been initialized with specific elements. Following this, a constant reverse iterator has been defined, starting from the beginning of the deque using the 'crbegin' function. Subsequently, a while loop is implemented, iterating until the 'citr' iterator reaches the 'c.crend' function. Within the loop, the iterator is shown, and it is incremented before display. The resulting output showcases the characters in reverse order, transforming 'laptop' into 'potpal'.
Let's consider a basic illustration to showcase the application of the crbegin and crend methods. In this scenario, we have instantiated a deque of type 'int' 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 illustration, a deque of integer type has been established, housing multiple integer values. Following this, a constant reverse iterator has been defined, initiated from the start of the deque through the utilization of the 'crbegin' function. Subsequently, a while loop has been implemented, iterating until the 'citr' iterator reaches the 'val.crend' function. Within the loop, the iterator's value is showcased, followed by a pre-increment operation on the iterator. The end result showcases the integer elements presented in reverse order, from the last to the first.
C++ crend function Example with a String Deque
Let's consider an illustration to showcase 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 instance, we instantiate a deque of strings named name with four elements. Subsequently, we employ a loop that iterates through the deque in reverse order using the crbegin and crend functions. This mechanism showcases the elements from the final to the initial position in reverse sequence. Given that both functions yield constant reverse iterators, the elements are accessible for reading purposes solely and cannot be altered throughout the traversal process.
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 summary, the deque::crend method in C++ is a straightforward and effective component of the Standard Template Library (STL) that allows for non-modifiable reverse traversal of a deque container. It is frequently used to provide a constant reverse iterator pointing to the position preceding the initial element, ensuring element immutability during iteration.
In C++, the crbegin function is commonly employed. This function offers a reliable and safe approach to retrieve deque elements in reverse sequence, aiding in the preservation of const-correctness and data integrity.
C++ Deque crend Function FAQs
The primary function of the deque crend method in C++ is to return a constant reverse iterator pointing to the theoretical element preceding the first element in a deque container.
The deque crend function in C++ is a built-in function of the deque container. It is frequently used to provide a constant reverse iterator pointing to the position before the first element, preventing any modifications to the elements during iteration.
The main contrast between the rend and crend functions in C++ lies in the type of reverse iterators they return.
In C++, the primary contrast between the rend method and the crend function lies in their functionalities. The rend function is typically employed to provide a reverseiterator for facilitating alterations, while the crend function is generally used to provide a constreverse_iterator for enabling solely read operations.
Can the crend function be used with non-const deques in C++?
Yes, the crend function in C++ can be employed with both constant and non-constant deque objects. It enables accessing the elements in a read-only manner in both scenarios.
4) Is it possible to make changes to elements using the iterator obtained from the crend function in C++?
No, altering the elements is not permitted as the crend function in C++ yields a constant reverse iterator, granting solely read-only access to the elements.
5) What is the time complexity of the crend method in C++?
When working with the deque crend function in C++, carrying out any operation will result in a time complexity of O(1), which indicates constant time.