c++- 迭代器与type_traits

备忘录:
写的不一定对

#include 
#include 

using namespace std;

struct INPUT_ITE_TAG {
};

struct OUTPUT_ITE_TAG {
};

struct FORWARD_ITE_TAG : public INPUT_ITE_TAG {
};

struct BI_ITE_TAG : public FORWARD_ITE_TAG {
};

struct RADON_ITE_TAG : public BI_ITE_TAG {
};


template<typename Ite, bool>
struct Ite_traits_imp {
};

template<typename Ite>
struct Ite_traits_imp<Ite, true> {
    typedef typename Ite::difference_type difference_type;
    typedef typename Ite::value_type value_type;
    typedef typename Ite::pointer pointer;
    typedef typename Ite::reference reference;
    typedef typename Ite::iterator_category iterator_category;
};


template<typename T>
struct _Has_Ite_typedef {
private:
    struct two {
        char left;
        char right;
    };

    template<class U>
    static two test(...);

    template<class U,
            typename = void_t<typename U::difference_type>,
            typename = void_t<typename U::value_type>,
            typename = void_t<typename U::pointer>,
            typename = void_t<typename U::reference>,
            typename = void_t<typename U::iterator_category>>
    static char test(int);

public:
    static constexpr bool value = sizeof(test<T>(0)) == 1;
};

template<typename Ite, bool>
struct _Ite_traits {
};

template<typename Ite>
struct _Ite_traits<Ite, true> : public Ite_traits_imp<
        Ite,
        is_convertible_v<typename Ite::iterator_category, INPUT_ITE_TAG> ||
        is_convertible_v<typename Ite::iterator_category, OUTPUT_ITE_TAG>
> {

};

template<typename Ite>
struct Ite_traits: public _Ite_traits<Ite, _Has_Ite_typedef<Ite>::value> {

};

template<typename Ite>
struct Ite_traits<Ite *> {
    typedef ptrdiff_t difference_type;
    typedef typename remove_cv<Ite>::type value_type;
    typedef Ite *pointer;
    typedef Ite &reference;
    typedef RADON_ITE_TAG iterator_category;
};


template<typename InputIte>
void AdvanceImp(InputIte &ite, typename Ite_traits<InputIte>::difference_type n, INPUT_ITE_TAG) {
    for (; n > 0; n--) ++ite;
}

template<typename BiIte>
void AdvanceImp(BiIte &ite, typename Ite_traits<BiIte>::difference_type n, BI_ITE_TAG) {
    if (n >= 0) for (; n > 0; n--) ++ite;
    else for (; n < 0; n++) --ite;
}

template<typename RanIte>
void AdvanceImp(RanIte &ite, typename Ite_traits<RanIte>::difference_type n, RADON_ITE_TAG) {
    ite += n;
}

template<typename Ite>
void Advance(Ite &ite, typename Ite_traits<Ite>::difference_type n) {
    AdvanceImp(ite, n, typename Ite_traits<Ite>::iterator_category{});
}

struct myListNode {
    int value;
    myListNode* next;

    myListNode() {
        value = -1;
        next = nullptr;
    }

    myListNode(int value, myListNode* next = nullptr) : value(value), next(next){}
};


struct myListIte {
    typedef ptrdiff_t difference_type;
    typedef int value_type;
    typedef int* pointer;
    typedef int& reference;
    typedef FORWARD_ITE_TAG iterator_category;

    myListNode* node;
    myListIte(myListNode* node = nullptr): node(node){}

    myListIte& operator++() {
        node = node->next;
        return *this;
    }

    friend bool operator==(const myListIte& lhs, const myListIte& rhs) {
        return lhs.node == rhs.node;
    }
};

struct myList{
    myListNode tail;
    myListNode* _beg = nullptr;
    myListNode* _end = nullptr;

    myList() {
        _beg = nullptr;
        _end = &tail;
    }

    myListIte begin() {
        return myListIte(_beg);
    }

    myListIte end() {
        return myListIte(_end);
    }
    void push_front(int value) {
        auto node = new myListNode(value);
        if (_beg) {
            node->next = _beg;
        }
        _beg = node;
    }
};

struct AIe {
    typedef int difference_type;
    typedef int value_type;
    typedef int* pointer;
    typedef int& reference;
    typedef INPUT_ITE_TAG iterator_category;
};

int main(int argc, char *argv[]) {
    myList list;
    list.push_front(1);
    list.push_front(2);
    list.push_front(3);

    for (auto ite = list.begin(); ite != list.end(); Advance(ite, 1)) {
        cout << ite.node->value << "\t" << endl;
    }
}

你可能感兴趣的:(c++,模板编程,c++,开发语言,算法)