Using a const_reverse_iterator to print value in vector in reverse - C++ STL

C++ examples for STL:reverse_iterator

Description

Using a const_reverse_iterator to print value in vector in reverse

Demo Code

#include <cinttypes>
#include <iostream>
#include <vector>

using namespace std;

int main(int arcg, char* argv[])
{
    using IntVector = vector<int32_t>;

    using IntVectorIterator = IntVector::iterator;
    using ConstIntVectorIterator = IntVector::const_iterator;

    using ReverseIntVectorIterator = IntVector::reverse_iterator;
    using ConstReverseIntVectorIterator = IntVector::const_reverse_iterator;

    IntVector myVector(5, 0);//from   w w  w .j  a  v  a  2s  .c o m
    int32_t value{ 0 };
    for (IntVectorIterator iter = myVector.begin(); iter != myVector.end(); ++iter)
    {
        *iter = value++;
    }
    for (ConstReverseIntVectorIterator iter = myVector.crbegin();
        iter != myVector.crend();
        ++iter)
    {
        cout << "The value is: " << *iter << endl;
    }

    return 0;
}

Result


Related Tutorials