In this article, we will discuss different ways to print the contents of a vector in reverse order.


Table of Contents

C++: Print all items of vector in reverse order using reverse iterator

In C++, vector provides two member functions which returns a reverse iterator,

  • rbegin() : Returns a reverse iterator pointing to the last element of the vector.
  • rend() : Returns a reverse iterator pointing to the element before the first element of the vector (theoriticaly).

We can use the reverse iterators returned by rbegin() and rend()  to iterate over the vector elements in reverse order and print them one by one. For example,

#include<iostream>
#include<vector>

/*
Iterate over the vector elements in reverse order and print 
them one by one.
*/
template <typename T>
void reverse_print(const std::vector<T> & vec, std::string sep=" ")
{
    // Iterate from end to start
    for (auto it = vec.rbegin(); it != vec.rend(); it++)
    {
        // Print each element
        std::cout << *it << sep;
    }
    std::cout << std::endl;
}

int main()
{
    // Vector of integers
    std::vector<int> vec_of_nums{1, 3, 4, 7, 8, 9};
    
    // Print all elements in vector in reverse order
    reverse_print(vec_of_nums, ",");

    return 0;
}

Output:

9,8,7,4,3,1,

C++: Print all items of a vector in reverse order using indexing

If you don’t want to use the reverse iterator, then you can use indexing to iterate over all elements of vector in reverse order and print them one by one. For example,

#include<iostream>
#include<vector>

/*
Iterate over the vector elements in reverse order and print 
them one by one.
*/
template <typename T>
void reverse_display(const std::vector<T> & vec, std::string sep=" ")
{
    // Iterate over elements of vector from end to start
    for(int i = vec.size() -1 ; i >= 0 ; i--)
    {
        // Print each element
        std::cout<<vec[i] << sep;
    }
    std::cout<<std::endl;
}

int main()
{
    // Vector of integers
    std::vector<int> vec_of_nums{1, 3, 4, 7, 8, 9};
    
    // Print all elements in vector in reverse order
    reverse_display(vec_of_nums, ",");

    return 0;
}

Output:

9,8,7,4,3,1,

C++: Print all elements of a vector in reverse order in one line

We can use the reverse iterators provided by rbegin() and rend() and push all elements between end of vector and start of vector to the output stream using the STL algorithm copy(). For example,

#include<iostream>
#include<vector>
#include<algorithm>
#include<iterator>

int main()
{
    // Vector of integers
    std::vector<int> vec_of_nums{1, 3, 4, 7, 8, 9};
    
    // Print all elements in vector in reverse order
    std::copy(  vec_of_nums.rbegin(),
                vec_of_nums.rend(),
                std::ostream_iterator<int>(std::cout," "));

    std::cout<<std::endl;

    return 0;
}

Output:

9 8 7 4 3 1

While passing the output stream object to the copy() function, we need to specify the type of elements that vector contains, like int in the above example. What if we want a generic one liner solution ?

This can be done using experimental::make_ostream_joiner intrduced in C++17. For example,

#include<iostream>
#include<vector>
#include<algorithm>
#include <experimental/iterator>

int main()
{
    // Vector of integers
    std::vector<int> vec_of_nums{1, 3, 4, 7, 8, 9};
    
    // Print all elements in vector in reverse order
    std::copy(  vec_of_nums.rbegin(),
                vec_of_nums.rend(),
                std::experimental::make_ostream_joiner(std::cout," ") );

    std::cout<<std::endl;

    return 0;
}

Output:

9 8 7 4 3 1

If you don’t want to use c++17 feature then you can use lambda function to create a generic solution

C++: Print all elements of a vector in reverse order using lambda function

We can iterate over all elements of vector in verse order using for_each() function and apply a lambda function on each of the element during iteration. For example,

#include<iostream>
#include<vector>
#include<algorithm>

int main()
{
    // Vector of integers
    std::vector<int> vec_of_nums{1, 3, 4, 7, 8, 9};
    
    // Print all elements in vector in reverse order
   std::for_each(  vec_of_nums.rbegin(),
                    vec_of_nums.rend(),
                    [](const auto & elem ) {
                            std::cout<<elem<<" ";
                    });

    std::cout<<std::endl;

    return 0;
}

Output:

9 8 7 4 3 1

It printed all the elements in vector in reverse order. This solution can work in any type of vector.

How did it work ?

Here for_each() function iterated over all elements of vector in reverse order, because we provided the reverse iterators as arguments i.e. starts from end and go till the starting of vector (in reverse order). Then for each element it called the passed lambda function, in which we printed the element.

Summary:

We discussed the different ways to print elements of vector in reverse order either by using range based for loops, indexing or generic one liner solutions.