C++ Online Compiler
Example: Sort String Descending with Reverse Iterators in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Sort String Descending with Reverse Iterators #include <iostream> #include <string> // Required for std::string #include <algorithm> // Required for std::sort using namespace std; int main() { // Step 1: Initialize the string string myString = "programming"; cout << "Original string: " << myString << endl; // Step 2: Sort the string in descending order using reverse iterators // std::rbegin() gives a reverse iterator to the last element. // std::rend() gives a reverse iterator to the element before the first. // std::sort sorts the range in ascending order of the reverse traversal, // which effectively sorts the original string in descending order. std::sort(myString.rbegin(), myString.rend()); // Step 3: Print the sorted string cout << "Sorted string (descending): " << myString << endl; return 0; }
Output
Clear
ADVERTISEMENTS