1// Using iterators
2for (auto it = s.crbegin() ; it != s.crend(); ++it) {
3 std::cout << *it;
4}
5
6// Naive
7for (int i = s.size() - 1; i >= 0; i--) {
8 std::cout << s[i];
9}
10
1// string::rbegin/rend
2#include <iostream>
3#include <string>
4
5int main ()
6{
7 std::string str ("now step live...");
8 for (std::string::reverse_iterator rit=str.rbegin(); rit!=str.rend(); ++rit)
9 std::cout << *rit;
10 return 0;
11}
1// A reverse_iterator example using vectors
2
3#include <iostream>
4#include <vector>
5
6int main() {
7 std::vector<int> vec = {1, 2, 3, 4, 5};
8 std::vector<int>::reverse_iterator r_iter;
9
10 // rbegin() points to the end of the vector, and rend()
11 // points to the front. Use crbegin() and crend() for
12 // the const versions of these interators.
13 for (r_iter = vec.rbegin(); r_iter != vec.rend(); r_iter++) {
14 std::cout << *r_iter << std::endl;
15 }
16
17 return 0;
18}