简体   繁体   中英

Custom iterator not dereferencing issue

Here is a first attempt at implementing an iterator to 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();
}

Expected Output:

aaaaaaaaaa

Instead of the above expected output I get:

Debug Assertion Failure
Expression: list iterator not dereferencable.

Why am I getting this behaviour and how to correct it?


Note: after a brief research I found that the most frequent cause for such a behaviour is an attempt at dereferencing the end() iterator, but I can't find such expression in my code.

You are dereferencing an end iterator *text.end() in Document::end() . The easiest thing to fix that would be using list::back() (and list::front() in Document::begin() ).

When you have fixed that, you will find that Text_iterator::operator++ will also dereference an end iterator, as you don't check ln against an appropriate end. @Jonathan Potter's comment is right, you need to pass text.end() to both Text_iterator s

Changes:

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());
}

When the final increment occurs, pos will point the end iterator of the final Line , and ln will point the end iterator of the text, which is what we passed to the Text_iterator constructor in Document::end() . We don't need to compare or expose Text_iterator::ln_end to retain sensible semantics.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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