Mega Code Archive

 
Categories / C++ / Algorithm
 

Use a reverse_iterator to go from the end to the beginning

#include <iostream> #include <list> #include <algorithm> #include <string> using namespace std; static const int ARRAY_SIZE = 5; int main( ) {    list<string> lstStr;    lstStr.push_back("A");    lstStr.push_back("B");    lstStr.push_back("C");    lstStr.push_back("D");    for (list<string>::iterator p = lstStr.begin( );         p != lstStr.end( ); ++p) {       cout << *p << endl;    }    for (list<string>::reverse_iterator p = lstStr.rbegin( );         p != lstStr.rend( ); ++p) {       cout << *p << endl;    } } /*  A B C D D C B A  */