Created
February 27, 2011 06:02
-
-
Save wilhelmtell/845950 to your computer and use it in GitHub Desktop.
n_iterator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iterator> | |
#include <iostream> | |
#include <vector> | |
#include <algorithm> | |
template<class I, class S> | |
struct n_iterator : public std::iterator_traits<I> { | |
n_iterator(); | |
n_iterator(I iter, S n); | |
template<class FI, class FS> friend | |
bool operator==(const n_iterator<FI,FS>& a, const n_iterator<FI,FS>& b); | |
template<class FI, class FS> friend | |
bool operator!=(const n_iterator<FI,FS>& a, const n_iterator<FI,FS>& b); | |
typename n_iterator::reference const operator*() const; | |
typename n_iterator::pointer operator->(); | |
n_iterator& operator++(); | |
n_iterator operator++(int); | |
// forward iterators | |
typename n_iterator::reference operator*(); | |
// bidirectional iterators | |
n_iterator& operator--(); | |
n_iterator operator--(int); | |
// random-access iterators | |
n_iterator operator+(typename n_iterator::difference_type d); | |
n_iterator operator-(typename n_iterator::difference_type d); | |
typename n_iterator::reference operator+=(typename n_iterator::difference_type d); | |
typename n_iterator::reference operator-=(typename n_iterator::difference_type d); | |
typename n_iterator::reference operator[](typename n_iterator::difference_type d); | |
template<class FI, class FS> friend | |
bool operator<(const n_iterator<FI,FS>& a, const n_iterator<FI,FS>& b); | |
template<class FI, class FS> friend | |
bool operator>(const n_iterator<FI,FS>& a, const n_iterator<FI,FS>& b); | |
template<class FI, class FS> friend | |
bool operator<=(const n_iterator<FI,FS>& a, const n_iterator<FI,FS>& b); | |
template<class FI, class FS> friend | |
bool operator>=(const n_iterator<FI,FS>& a, const n_iterator<FI,FS>& b); | |
private: | |
I iter; | |
S n; | |
}; | |
template<class I, class S> | |
n_iterator<I,S>::n_iterator() | |
{ | |
} | |
template<class I, class S> | |
n_iterator<I,S>::n_iterator(I iter, S n) | |
: iter(iter), n(n) | |
{ | |
} | |
template<class I, class S> | |
typename n_iterator<I,S>::reference const n_iterator<I,S>::operator*() const | |
{ | |
return *iter; | |
} | |
template<class I, class S> | |
typename n_iterator<I,S>::pointer n_iterator<I,S>::operator->() | |
{ | |
return &*iter; | |
} | |
template<class I, class S> | |
n_iterator<I,S>& n_iterator<I,S>::operator++() | |
{ | |
--n; ++iter; | |
return *this; | |
} | |
template<class I, class S> | |
n_iterator<I,S> n_iterator<I,S>::operator++(int) | |
{ | |
n_iterator cp(*this); | |
++*this; | |
return cp; | |
} | |
template<class I, class S> | |
typename n_iterator<I,S>::reference n_iterator<I,S>::operator*() | |
{ | |
return *iter; | |
} | |
template<class I, class S> | |
n_iterator<I,S>& n_iterator<I,S>::operator--() | |
{ | |
++n; --iter; | |
return *this; | |
} | |
template<class I, class S> | |
n_iterator<I,S> n_iterator<I,S>::operator--(int) | |
{ | |
n_iterator cp(*this); | |
--*this; | |
return cp; | |
} | |
template<class I, class S> | |
n_iterator<I,S> | |
n_iterator<I,S>::operator+(typename n_iterator<I,S>::difference_type d) | |
{ | |
return iter + d; | |
} | |
template<class I, class S> | |
n_iterator<I,S> | |
n_iterator<I,S>::operator-(typename n_iterator<I,S>::difference_type d) | |
{ | |
return iter - d; | |
} | |
template<class I, class S> | |
typename n_iterator<I,S>::reference | |
n_iterator<I,S>::operator[](typename n_iterator<I,S>::difference_type d) | |
{ | |
return iter[d]; | |
} | |
template<class I, class S> | |
bool operator==(const n_iterator<I,S>& a, const n_iterator<I,S>& b) | |
{ | |
return a.n == b.n; | |
} | |
template<class I, class S> | |
bool operator!=(const n_iterator<I,S>& a, const n_iterator<I,S>& b) | |
{ | |
return !(a == b); | |
} | |
template<class I, class S> | |
bool operator<(const n_iterator<I,S>& a, const n_iterator<I,S>& b) | |
{ | |
return a.iter < b.iter; | |
} | |
template<class I, class S> | |
bool operator>(const n_iterator<I,S>& a, const n_iterator<I,S>& b) | |
{ | |
return a.iter > b.iter; | |
} | |
template<class I, class S> | |
bool operator<=(const n_iterator<I,S>& a, const n_iterator<I,S>& b) | |
{ | |
return a.iter <= b.iter; | |
} | |
template<class I, class S> | |
bool operator>=(const n_iterator<I,S>& a, const n_iterator<I,S>& b) | |
{ | |
return a.iter >= b.iter; | |
} | |
template<class I, class S> | |
typename n_iterator<I,S>::reference n_iterator<I,S>::operator+=(typename n_iterator::difference_type d) | |
{ | |
n -= d; | |
return iter += d; | |
} | |
template<class I, class S> | |
typename n_iterator<I,S>::reference n_iterator<I,S>::operator-=(typename n_iterator::difference_type d) | |
{ | |
n += d; | |
return iter -= d; | |
} | |
template<class I, class S> | |
n_iterator<I,S> n_iter(I iter, S n) | |
{ | |
return n_iterator<I,S>(iter, n); | |
} | |
int f() { return 3; } | |
int main(int argc, char* argv[]) | |
{ | |
typedef std::vector<int> cont; | |
cont c(5); | |
std::generate(n_iter(c.begin(), 2), n_iter(c.begin(), 0), f); | |
std::copy(c.begin(), c.end(), std::ostream_iterator<int>(std::cout, " ")); | |
std::cout << '\n'; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment