列出地图实现中的迭代器实现 - 麻烦

时间:2016-01-12 20:24:30

标签: c++ list dictionary iterator nodes

模板类中的迭代器实现DataType的列表。

 template <DataType>
 class List{
        ...

    class myIterator
    {
    public:
        typedef myIterator self_type;
        typedef Node<DataType>* pointer;

        myIterator(pointer ptr) : ptr_(ptr) { }
        self_type operator++() {
            self_type i = *this;
            if (ptr_) ptr_ = ptr_->Next();
            return i;
        }
        int operator*() { if (ptr_ ) return ptr_->Data(); else return 0; }
        bool operator==(const self_type& rhs) {
            if (!ptr_ && !rhs.ptr_) return true;
            return (ptr_ && rhs.ptr_ && rhs.ptr_->Data()==ptr_->Data());
        }
        bool operator!=(const self_type& rhs) {
            return !(*this==rhs);
        }
    private:
        pointer ptr_ = nullptr;
    };

    myIterator begin() { return myIterator(head); }
    myIterator end() { return myIterator(nullptr); }
};

使用:

void remove(const KeyType& key) {
            if (!this->containsKey(key)) {
                return;
            }
            for (List<Pair, CompareFunction>::myIterator it = this->_pairs.begin();
            it != this->_pairs.end(); ++it) {
                Pair cur_pair = *it;
                if (cur_pair.first == key) {
                    this->_pairs.Delete(cur_pair);
                    this->_size--;
                }
            }
        }

此func标题的相关代码:

template <class KeyType, class ValueType, class CompareFunction = std::less<KeyType> >
    class MtmMap {

    public:
        class Pair {
        public:
            Pair() :first(KeyType()) {} ////////////////////
            Pair(const KeyType& key, const ValueType& value)
                : first(key), second(value) {}

            const KeyType first;
            ValueType second;

            ~Pair() = default;

            Pair& operator=(const Pair& pair) {
                this->second = pair.second;
                return *this;
            }
        };

目的: 我在该循环中尝试做的是迭代整个列表并找到该对,其中键与给定的相同,然后删除整个对。

问题:

*it

被解释为int,在我们的例子中我猜想从MtmMap泄漏,因为在我的“主要”中:

typedef MtmMap<int, int> IntMap;
typedef IntMap::Pair IntPair;

IntMap map1(10);
map1.insert(IntPair(1, 2));
map1.remove(1);

错误:

" cannot convert from "int" to "mtm::MtmMap<int,int,std::less<KeyType>>::Pair" ""

修改 我还有另一个问题,就是下一段代码:

const ValueType& operator[](const KeyType& key) const {
            for (List<Pair, CompareFunction>::myIterator it = this->_pairs.begin();
            it != this->_pairs.end(); ++it) {

在使用迭代器时,编译器会喊:

Error   C2662   List<mtm::MtmMap<int,int,std::less<KeyType>>::Pair,CompareFunction>::myIterator List<mtm::MtmMap<KeyType,int,CompareFunction>::Pair,CompareFunction>::begin(void):  cannot convert 'this' from "const List<mtm::MtmMap<int,int,std::less<KeyType>>::Pair,CompareFunction>" to "List<mtm::MtmMap<int,int,std::less<KeyType>>::Pair,CompareFunction> &"

据我所知,这里存在const-correctness的问题。我不能使迭代器const,然后我将无法迭代它。我该如何处理?

1 个答案:

答案 0 :(得分:1)

我认为缺少一些细节,但

int operator*() { if (ptr_ ) return ptr_->Data(); else return 0; }

仅在ptr _-&gt; Data()为int时才有效,这可能不是您的意图。返回值不应该是DataType吗?

相关问题