iterate vector from end to begin 1

iterate vector from end to begin

for ( vector::reverse_iterator i = my_vector.rbegin(); 
        i != my_vector.rend(); ++i ) { 
}

OR

vector::iterator i = my_vector.end();
while (i != my_vector.begin())
{
     --i;
    /*do stuff */

} 

Here is what the above code is Doing:
1. The first line creates a reverse iterator that points to the last element in the vector.
2. The second line checks to see if the iterator is pointing to the first element in the vector.
3. The third line moves the iterator back one element.
4. The fourth line does something with the element that the iterator is pointing to.
5. The fifth line repeats the process until the iterator is pointing to the first element in the vector.

Similar Posts