Skip to content

Instantly share code, notes, and snippets.

@xfbs
Created January 26, 2018 22:09
Show Gist options
  • Save xfbs/f8c549a410a966860634fa4ab0858adf to your computer and use it in GitHub Desktop.
Save xfbs/f8c549a410a966860634fa4ab0858adf to your computer and use it in GitHub Desktop.
map_iter: (broken) map iterator in C++.
template<class InputIt>
using value_type = typename std::iterator_traits<InputIt>::value_type;
template<class InputIt>
using iterator_category = typename std::iterator_traits<InputIt>::iterator_category;
template<class InputIt>
using difference_type = typename std::iterator_traits<InputIt>::difference_type;
template<class InputIt>
using pointer = typename std::iterator_traits<InputIt>::pointer;
template<class InputIt>
using reference = typename std::iterator_traits<InputIt>::reference;
template<class IterType, class Fun>
class map_iter:
public std::iterator<iterator_category<IterType>,
value_type<IterType>,
difference_type<IterType>,
pointer<IterType>,
reference<IterType>>
{
IterType iter;
Fun fun;
explicit map_iter(IterType _iter): iter(_iter), fun([](){}) {}
explicit map_iter(IterType _iter, Fun f): iter(_iter), fun(f) {}
map_iter& operator++() { ++iter; return *this; }
map_iter operator++(int) { map_iter prev = *this; ++iter; return prev; }
bool operator==(map_iter other) const { return iter == other.iter; }
bool operator!=(map_iter other) const { return iter != other.iter; }
bool operator==(IterType other) const { return iter == other; }
bool operator!=(IterType other) const { return iter != other; }
reference<IterType> operator*() const { return fun(*iter); }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment