自定义迭代器而不是解释问题

Custom iterator not dereferencing issue

本文关键字:解释 问题 迭代器 自定义      更新时间:2023-10-16

这是将迭代器实现至 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();
}

预期输出:

aaaaaaaaaaa

而不是上述预期输出,我得到了:

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

为什么我得到这种行为以及如何纠正?


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

您在 Document::end()中删除了结束迭代器*text.end()。最简单的修复方法是使用list::back()(和Document::begin()中的list::front())。

解决此问题后,您会发现Text_iterator::operator++还将放置结束迭代器,因为您不会在适当的端情况下检查ln。@Jonathan Potter的评论是对的,您需要将text.end()传递给两个Text_iterator S

更改:

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将指向文本的末端迭代器,这就是我们将其传递给Text_iterator构造函数中的Document::end()中的内容。我们不需要比较或暴露Text_iterator::ln_end来保留明智的语义。