自定义容器的STL兼容迭代器 [英] STL-compatible iterators for custom containers

查看:288
本文介绍了自定义容器的STL兼容迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的容器,我已经使用了很多年没有问题。最近我发现如果我为我的容器定义迭代器,我可以有效地使用< algorithm> 中定义的所有算法。不仅如此,似乎推力库(基本上认为CUDA版本的STL为Nvidia GPU)大量使用迭代器,我希望通过使用

I have a custom container that I have been using for many years without issues. Recently I found out that if I define iterators for my container, I can effectively use all of the algorithms defined in <algorithm>. Not only that, it seems that thrust library (basically think CUDA version of STL for Nvidia GPUs) heavily uses iterators and I am hoping that by using them I'll be able to use that library as well.

无论如何,因为这是我第一次尝试写我自己的迭代器,我想我发布我在这里要求进一步的帮助,并确保我在做什么是正确的。所以,我写了一个小数组类,支持 iterator const_iterator 类。我跑了我的类与一堆不同的STL算法和所有似乎工作正常,但这并不一定意味着我有一切正确!特别是,有没有任何操作符,我错过我的迭代器?
我定义了额外的不必要的吗?另外,由于 iterator const_iterator 的大部分看起来类似,有没有办法防止重复?

Anyway, since this is my first try at writing my own iterators, I thought I post what I have here to ask for further assistance and make sure what I'm doing is right. So, I wrote a little array class that supports both iterator and const_iterator classes. I ran my class with a bunch of different STL algorithms and all seem to work fine but that does not necessarily mean I've got everything right! In particular, is there any operator that I miss for my iterators? Have I defined extra unnecessary ones? Also, since most of iterator and const_iterator look similar, is there a way to prevent duplication?

我乐意接受建议和改进:)

I'm open to suggestions and improvements :)

Live example:http://ideone.com/7YdiQY

Live example: http://ideone.com/7YdiQY

#include <cstddef>
#include <iostream>
#include <iterator>
#include <algorithm>

template<typename T>
class my_array{
    T* data_;
    std::size_t size_;

public:

    // ---------------------------------
    // Forward declaration
    // ---------------------------------
    class const_iterator;

    // ---------------------------------
    // iterator class
    // ---------------------------------
    class iterator: public std::iterator<std::random_access_iterator_tag, T>
    {
    public:
        iterator(): p_(NULL) {}
        iterator(T* p): p_(p) {}
        iterator(const iterator& other): p_(other.p_) {}
        const iterator& operator=(const iterator& other) {p_ = other.p_; return other;}

        iterator& operator++()    {p_++; return *this;} // prefix++
        iterator  operator++(int) {iterator tmp(*this); ++(*this); return tmp;} // postfix++
        iterator& operator--()    {p_--; return *this;} // prefix--
        iterator  operator--(int) {iterator tmp(*this); --(*this); return tmp;} // postfix--

        void     operator+=(const std::size_t& n)  {p_ += n;}
        void     operator+=(const iterator& other) {p_ += other.p_;}
        iterator operator+ (const std::size_t& n)  {iterator tmp(*this); tmp += n; return tmp;}
        iterator operator+ (const iterator& other) {iterator tmp(*this); tmp += other; return tmp;}

        void        operator-=(const std::size_t& n)  {p_ -= n;}
        void        operator-=(const iterator& other) {p_ -= other.p_;}
        iterator    operator- (const std::size_t& n)  {iterator tmp(*this); tmp -= n; return tmp;}
        std::size_t operator- (const iterator& other) {return p_ - other.p_;}

        bool operator< (const iterator& other) {return (p_-other.p_)< 0;}
        bool operator<=(const iterator& other) {return (p_-other.p_)<=0;}
        bool operator> (const iterator& other) {return (p_-other.p_)> 0;}
        bool operator>=(const iterator& other) {return (p_-other.p_)>=0;}
        bool operator==(const iterator& other) {return  p_ == other.p_; }
        bool operator!=(const iterator& other) {return  p_ != other.p_; }

        T& operator[](const int& n) {return *(p_+n);}
        T& operator*() {return *p_;}
        T* operator->(){return  p_;}

    private:
        T* p_;

        friend class const_iterator;
    };

    // ---------------------------------
    // const_iterator class
    // ---------------------------------
    class const_iterator: public std::iterator<std::random_access_iterator_tag, T>
    {
    public:
        const_iterator(): p_(NULL) {}
        const_iterator(const T* p): p_(p) {}
        const_iterator(const iterator& other): p_(other.p_) {}
        const_iterator(const const_iterator& other): p_(other.p_) {}
        const const_iterator& operator=(const const_iterator& other) {p_ = other.p_; return other;}
        const const_iterator& operator=(const iterator& other) {p_ = other.p_; return other;}

        const_iterator& operator++()    {p_++; return *this;} // prefix++
        const_iterator  operator++(int) {const_iterator tmp(*this); ++(*this); return tmp;} // postfix++
        const_iterator& operator--()    {p_--; return *this;} // prefix--
        const_iterator  operator--(int) {const_iterator tmp(*this); --(*this); return tmp;} // postfix--

        void           operator+=(const std::size_t& n)              {p_ += n;}
        void           operator+=(const const_iterator& other)       {p_ += other.p_;}
        const_iterator operator+ (const std::size_t& n)        const {const_iterator tmp(*this); tmp += n; return tmp;}
        const_iterator operator+ (const const_iterator& other) const {const_iterator tmp(*this); tmp += other; return tmp;}

        void           operator-=(const std::size_t& n)              {p_ -= n;}
        void           operator-=(const const_iterator& other)       {p_ -= other.p_;}
        const_iterator operator- (const std::size_t& n)        const {const_iterator tmp(*this); tmp -= n; return tmp;}
        std::size_t    operator- (const const_iterator& other) const {return p_ - other.p_;}

        bool operator< (const const_iterator& other) const {return (p_-other.p_)< 0;}
        bool operator<=(const const_iterator& other) const {return (p_-other.p_)<=0;}
        bool operator> (const const_iterator& other) const {return (p_-other.p_)> 0;}
        bool operator>=(const const_iterator& other) const {return (p_-other.p_)>=0;}
        bool operator==(const const_iterator& other) const {return  p_ == other.p_; }
        bool operator!=(const const_iterator& other) const {return  p_ != other.p_; }

        const T& operator[](const int& n) const {return *(p_+n);}
        const T& operator*()  const {return *p_;}
        const T* operator->() const {return  p_;}

    private:
        const T* p_;
    };

    my_array()
        : data_(NULL), size_(0)
    {}
    my_array(std::size_t size)
        : data_(new T[size]), size_(size)
    {}
    my_array(const my_array<T>& other){
        size_ = other.size_;
        data_ = new T[size_];
        for (std::size_t i = 0; i<size_; i++)
            data_[i] = other.data_[i];
    }
    my_array(const const_iterator& first, const const_iterator& last){
        size_ = last - first;
        data_ = new T[size_];

        for (std::size_t i = 0; i<size_; i++)
            data_[i] = first[i];
    }

    ~my_array(){
        delete [] data_;
    }
    const my_array<T>& operator=(const my_array<T>& other){
        size_ = other.size_;
        data_ = new T[size_];
        for (std::size_t i = 0; i<size_; i++)
            data_[i] = other.data_[i];
        return other;
    }
    const T& operator[](std::size_t idx) const {return data_[idx];}
    T& operator[](std::size_t& idx) {return data_[idx];}
    std::size_t size(){return size_;}

    iterator begin(){ return iterator(data_); }
    iterator end()  { return iterator(data_+size_); }
    const_iterator begin() const{ return const_iterator(data_); }
    const_iterator end() const  { return const_iterator(data_+size_);}
};

template<typename T>
void print(T t) {
    std::cout << t << std::endl;
}

int main(){

    // works!
    int list [] = {1, 3, 5, 2, 4, 3, 5, 10, 10};
    my_array<int> a(list, list+sizeof(list)/sizeof(int));

    // works!
    for (my_array<int>::const_iterator it = a.begin(), end = a.end();
         it != end; ++it)
        std::cout << ' ' << *it;
    std::cout << std::endl;

    // works!
    std::for_each(a.begin(), a.end(), print<int>);
    std::cout << std::endl;

    // works!
    my_array<int> b(a.size());
    std::copy(a.begin(), a.end(), b.begin());

    // works!
    my_array<int>::iterator end = std::remove(a.begin(), a.end(), 5);
    std::for_each(a.begin(), end, print<int>);
    std::cout << std::endl;

    // works!
    std::random_shuffle(a.begin(), end);
    std::for_each(a.begin(), end, print<int>);
    std::cout << std::endl;

    // works!
    std::cout << "Counts of 3 in array = " << std::count(a.begin(), end, 3) << std::endl << std::endl;

    // works!
    std::sort(a.begin(), end);
    std::for_each(a.begin(), end, print<int>);
    std::cout << std::endl;

    // works!
    if (!std::binary_search(a.begin(), a.end(), 5))
        std::cout << "Removed!" << std::endl;

    return 0;
}


推荐答案

boost迭代器提供了一个框架来创建符合stl的迭代器,并适应现有

boost iterator provides a framework to create stl-compliant iterators and to adapt existing ones.

它允许您专注于功能,并生成所有必要的特性,typedefs。

It allows you to focus on the functionality and generates all necessary traits, typedefs for you.

迭代器 const_iterator 创建时没有太多的代码重复也被支持。

iterator and const_iterator creation without much code-duplication is supported as well.

这篇关于自定义容器的STL兼容迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆