reverse_iterate.h 783 Bytes
Newer Older
barrystyle's avatar
barrystyle committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
// Copyright (c) 2017 The PIVX Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#pragma once
/*
    Template used for reverse iteration in C++11 range-based for loops.
    std::vector<int> v = {1, 2, 3, 4, 5};
    for (auto x : reverse_iterate(v))
        std::cout << x << " ";
 */

template <typename T>
class reverse_range
{
    T &x;
    
public:
    reverse_range(T &x) : x(x) {}
    
    auto begin() const -> decltype(this->x.rbegin())
    {
        return x.rbegin();
    }
    
    auto end() const -> decltype(this->x.rend())
    {
        return x.rend();
    }
};
 
template <typename T>
reverse_range<T> reverse_iterate(T &x)
{
    return reverse_range<T>(x);
}