【问题标题】:What is wrong with my custom iterator [closed]我的自定义迭代器有什么问题[关闭]
【发布时间】:2012-09-24 16:37:49
【问题描述】:

所以,我一直在试图弄清楚自定义迭代器是如何工作的,在投入了这么多时间但仍然不够用之后,我决定问问整个社区:我做错了什么?请帮忙,解释会很好,但如果是“次要”,则没有必要。

作为一个附带问题,有谁知道让“显示为集合关联”在 MSVS 的类图中工作的要求是什么?它只是不适用于c ++吗?就它而言,甚至我的 bar2 也不是整数的集合。

这是我的代码,它应该可以编译,但显然我的迭代器坏了......

编辑:既然人们在问,问题是我实现的迭代器没有迭代 Bar

#include <vector>
#include <iostream>

using namespace std;

template<class T> 
class Foo
{
public:
    template<class T>

    class FooIterator : std::iterator<std::forward_iterator_tag, T, ptrdiff_t, T*, T&>
    {
    public:
        Foo<T>* arrayPtr;
        int index, size;
        FooIterator(Foo<T>* arrayPtr, int size, int index) : arrayPtr(arrayPtr), size(size), index(index) {}

        T& operator*() {return *(arrayPtr->operator[](index));}
        T* operator->() {return &(operator*());}

        FooIterator &operator++(){++index; return *this;}
        FooIterator operator++(int){FooIterator tmp(*this); ++(*this); return tmp;}
        bool operator!=(const FooIterator &other) const {return other.index == index;}
    };

    T** ts;
    int size;
    int index;

    typedef FooIterator<T> fooiterator;
    fooiterator begin(){return fooiterator(this, size, 0);}
    fooiterator end(){return fooiterator(this, size, size);}

    ~Foo();

    void init(int size);
    void insert(T* item);
    T* operator[](int index);

    typedef T          value_type;
    typedef T*         pointer;
    typedef const T*   const_pointer;
    typedef T&         reference;
    typedef const T&   const_reference;
    typedef int        size_type;
    typedef ptrdiff_t  difference_type;
};

template<class T>
void Foo<T>::init(int size)
{
    ts = new T*[size];
    index = 0;
}

template<class T>
Foo<T>::~Foo()
{
    for(int i = 0; i < index; i++)
        delete ts[i];
    delete ts;
}

template<class T>
void Foo<T>::insert(T* item)
{
    ts[index++] = item;
}

template<class T>
T* Foo<T>::operator[](int index)
{
    return ts[index];
}

struct Bar
{
public:
    Foo<int> nums;
    Bar()
    {
        nums.init(3);
        int val = 1;
        nums.insert(new int(1));
        nums.insert(new int(2));
        nums.insert(new int(3));

        for each (int var in nums)
        {
            cout << var << endl;
        }
    }
};

struct Bar2
{
    vector<int> nums;
    Bar2()
    {

        nums.push_back(4);
        nums.push_back(5);
        nums.push_back(6);
        for each (int var in nums)
        {
            cout << var << endl;
        }
    }
};

int main ()
{
    Bar bar;
    /*for (int i = 0; i < bar.nums.index; i++)
    {
        cout << *bar.nums[i] << endl;
    }*/
    Bar2 bar2;
    cin.get();

    return 0;
}

【问题讨论】:

  • 我会问同样的问题...你能告诉我们它有什么问题吗?后编译错误/问题描述
  • 不,它不编译。有一个(不必要的阴影模板参数)并且for each 不是 C++。
  • @pmr,每个都是visual c++ feature
  • 问题是 bar1 中的 for each 实际上并没有打印任何东西。迭代器没有迭代...
  • this 似乎是开始实现自己的迭代器的好方法。

标签: c++ visual-c++ iterator


【解决方案1】:

一个明显的问题是operator!= 实际上测试是否相等。

【讨论】:

  • 应该怎么做?我非常想遵循我见过的例子......哦,nvm lol
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-25
  • 1970-01-01
  • 2013-02-11
  • 1970-01-01
  • 2023-04-01
  • 2015-04-29
  • 2014-12-07
相关资源
最近更新 更多