繁体   English   中英

自定义迭代器未取消引用问题

[英]Custom iterator not dereferencing issue

这是实现std::list<std::vector<char>>的迭代器的第一次尝试:

Document.h

#ifndef Document_h
#define Document_h

//-------------------------------------------------------------------------

typedef std::vector<char> Line;                                 // line of text

//-------------------------------------------------------------------------

class Text_iterator
{
public:
    Text_iterator(std::list<Line>::iterator l, Line::iterator p)// constructor
        : ln(l), pos(p) { }

    Text_iterator(const Text_iterator& src)                     // copy constructor
        : ln(src.ln), pos(src.pos) { }

    Text_iterator& operator= (const Text_iterator& src)         // copy assignment
    {
        Text_iterator temp(src);
        this->swap(temp);
        return *this;
    }

    char& operator*() { return *pos; }                          // dereferencing

    Text_iterator& operator++ ()                                // incrementation
    {
        ++pos;
        if (pos == ln->end())        
        {
            ++ln;
            pos = ln->begin();
        }
        return *this;
    }

    bool operator== (const Text_iterator& other) const          // comparison
    {
        return ln == other.ln && pos == other.pos;
    }

    bool operator != (const Text_iterator& other) const         // comparison
    {
        return !(*this == other);
    }

    void swap(Text_iterator& src)                               // helper: swap
    {
        std::swap(src.get_line(), ln);
        std::swap(src.get_column(), pos);
    }

    std::list<Line>::iterator get_line() { return ln; }         // accessors
    Line::iterator get_column() { return pos; }                             

private:
    std::list<Line>::iterator ln;                               // data members
    Line::iterator pos;
};

//-------------------------------------------------------------------------

void swap (Text_iterator& lhs, Text_iterator& rhs)              // object swap
{
    lhs.swap(rhs);
}

//-------------------------------------------------------------------------

class Document
{
public:
    typedef Text_iterator iterator;
public:
    Document()                                                  // constructor
    {
        Line l(10, 'a');
        text.push_back(l);
    }

    iterator begin()                                            // iterator to first element
    { 
        return iterator(text.begin(), (*text.begin()).begin());
    }

    iterator end()                                              // iterator to last element
    { 
        return iterator(text.end(), (*text.end()).end());
    }

    void print()
    { 
        for (Document::iterator p = begin(); p != end(); ++p)
        {
            std::cout << *p;
            getchar();
        }
    }

    std::list<Line> text;                                       // data member
};

#endif

main.cpp

#include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include <algorithm>
#include "Document.h"

int main()
{
    Document text;
    text.print();
}

预期产量:

啊啊啊啊啊啊

代替上面的预期输出,我得到:

调试断言失败
表达式:列表迭代器不可引用。

为什么会出现这种现象以及如何纠正它?


注意:经过简短的研究,我发现导致这种行为的最常见原因是尝试取消引用end()迭代器,但是在我的代码中找不到这种表达。

您要在Document::end()中取消引用结束迭代器*text.end() Document::end() 最简单的解决方法是使用list::back() (和Document::begin() list::front() Document::begin() )。

修复此问题后,您会发现Text_iterator::operator++还将取消引用结束迭代器,因为您没有针对适当的结束检查ln @Jonathan Potter的评论是正确的,您需要将text.end()传递给两个Text_iterator

变化:

class Text_iterator
{
    // Declarations elided
private:
    std::list<Line>::iterator ln;
    std::list<Line>::iterator ln_end;
    Line::iterator pos;        
}

Text_iterator::Text_iterator(std::list<Line>::iterator l, std::list<Line>::iterator l_end, Line::iterator p)
    : ln(l), ln_end(l_end), pos(p) { }

Text_iterator::Text_iterator(const Text_iterator& src)
    : ln(src.ln), ln_end(src.ln_end), pos(src.pos) { }

Text_iterator& Text_iterator::operator++ ()
{
    ++pos;
    if (pos == ln->end())        
    {
        ++ln;
        if(ln != ln_end)
        {
            pos = ln->begin();
        }
    }
    return *this;
}

void Text_iterator::swap(Text_iterator& src)
{
    std::swap(src.ln, ln);
    std::swap(src.ln_end, ln_end);
    std::swap(src.pos, pos);
}

Document::iterator Document::begin()
{ 
    return iterator(text.begin(), text.end(), text.front().begin());
}

Document::iterator Document::end()
{ 
    return iterator(text.end(), text.end(), text.back().end());
}

当最终增量出现时,pos将指向最后Line的结束迭代器,ln将指向文本的结束迭代器,这就是我们在Document::end()传递给Text_iterator构造函数的内容。 我们不需要比较或公开Text_iterator::ln_end即可保留明智的语义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM