template <class Category, class T, class Distance = ptrdiff_t,
class Pointer = T*, class Reference = T&>
struct iterator
{
typedef Category iterator_category;
typedef T value_type;
typedef Distance difference_type;
typedef Pointer pointer;
typedef Reference reference;
};
별로 설명할게없음.
std::ptrdiff_t 도 따로했고, category도 정리해둠.
어쨋든, iterator 구조체는 나중에, reverse_iterator에서 사용됨.
https://wetodev.rd.tuni.fi/wetoextra/cpp/en/cpp/iterator/reverse_iterator.html
template< class Iterator >
class reverse_iterator : public std::iterator<
typename std::iterator_traits<Iterator>::iterator_category,
typename std::iterator_traits<Iterator>::value_type,
typename std::iterator_traits<Iterator>::difference_type,
typename std::iterator_traits<Iterator>::pointer,
typename std::iterator_traits<Iterator>::reference >
반복자와 배열의 포인터를 구분하기위해서 만들어진것!
template<class Iter>
struct iterator_traits {
public:
typedef typename Iter::iterator_category terator_category;
typedef typename Iter::difference_type difference_type;
typedef typename Iter::value_type value_type;
typedef typename Iter::pointer pointer;
typedef typename Iter::reference reference;
};
// iterator traits for pointer
/*
int main()
{
typedef std::iterator_traits<int*> traits;
if (typeid(traits::iterator_category)==typeid(std::random_access_iterator_tag))
std::cout << "int* is a random-access iterator";
return 0;
}
*/
// container를 위한 iterator_traits
template<class T>
struct iterator_traits<T *>
{
typedef random_access_iterator_tag iterator_category;
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef ptrdiff_t distance_type;
typedef T *pointer;
typedef T &reference;
};
// iterator traits for const pointer
template<class T>
struct iterator_traits<const T *>
{
typedef random_access_iterator_tag iterator_category;
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef ptrdiff_t distance_type;
typedef T *pointer;
typedef T &reference;
};