mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-10 22:18:54 +01:00
Introduce src/reverse_iterator.hpp and include it...
...where it will be needed Taken from https://gist.github.com/arvidsson/7231973 with small modifications to fit the bitcoin core project
This commit is contained in:
39
src/reverse_iterator.h
Normal file
39
src/reverse_iterator.h
Normal file
@@ -0,0 +1,39 @@
|
||||
// Taken from https://gist.github.com/arvidsson/7231973
|
||||
|
||||
#ifndef BITCOIN_REVERSE_ITERATOR_HPP
|
||||
#define BITCOIN_REVERSE_ITERATOR_HPP
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
#endif // BITCOIN_REVERSE_ITERATOR_HPP
|
||||
Reference in New Issue
Block a user