reverse iterator c++ code example
Example 1: for loop reverse C++
for (auto it = s.crbegin() ; it != s.crend(); ++it) {
std::cout << *it;
}
for (int i = s.size() - 1; i >= 0; i--) {
std::cout << s[i];
}
Example 2: reverse iterator c++
#include <iostream>
#include <vector>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
std::vector<int>::reverse_iterator r_iter;
for (r_iter = vec.rbegin(); r_iter != vec.rend(); r_iter++) {
std::cout << *r_iter << std::endl;
}
return 0;
}
Example 3: string reverse iterator c++
#include <iostream>
#include <string>
int main ()
{
std::string str ("now step live...");
for (std::string::reverse_iterator rit=str.rbegin(); rit!=str.rend(); ++rit)
std::cout << *rit;
return 0;
}
Example 4: string reverse iterator c++
...evil pets won