c++ - 检查迭代器

标签 c++ stl iterator

我正在尝试测试 Stroustrup 的 C++ 书中的“检查迭代器”示例。该代码有一个运行时错误:“列表迭代器不兼容”。

错误是由函数“if( c->end() == p )”的“valid()”触发的。这个错误是什么意思?我猜都是c->end()p应该是 list<int>::iterator 类型.

另外,我不明白为什么要这样实现“operator”。顺便说一句,我正在使用 VS2008。

struct out_of_bounds{
    out_of_bounds() {}
};

template<class Cont, class Iter = typename Cont::iterator>
class Checked_iter : public iterator_traits<Iter> // deriving from iterator_traits
{
    Iter curr;   // iterator for current position
    Cont* c;    // pointer to current container
    // ...
public:

    void valid(Iter p){
        if ( c->end() == p )
            return;
        for (Iter pp = c->begin(); pp != c->end(); ++pp ){
            if ( pp == p )
                return;
        }
        throw out_of_bounds();
    }

    friend bool operator==(const Checked_iter& i, const Checked_iter& j){
        return i.c == j.c && i.curr == j.curr;
    }

    // no default initializer
    // use default copy constructor and copy assignment
    Checked_iter(Cont x, Iter p) : c(&x), curr(p) { valid(p); }

    reference operator*(){
        if ( curr == c->end() ) throw out_of_bounds();
        return *curr;
    }

    pointer operator->(){
        return &*curr; // checked by *
    }

    Checked_iter operator+(difference_type d){ // for random-access iterator only
        if ( c->end() - curr <= d  )
            throw out_of_bounds();
        return Checked_iter(c, curr+d);
    }

    reference operator[](difference_type d){ // for random-access iterator only
        if ( c->end() - curr <= d  ) throw out_of_bounds();
        return c[d]; 
    }

    Checked_iter& operator++(){ // prefix ++
        if ( curr == c->end() ) throw out_of_bounds();
        ++curr;
        return *this;
    }

    Checked_iter& operator++(int) { // postfix ++
        Checked_iter temp = *this;
        ++*this; // checked by prefix ++
        return temp;
    }

    Checked_iter& operator--() { // prefix --
        if ( curr == c->begin() ) throw out_of_bounds();
        --curr;
        return *this;
    }

    Checked_iter& operator--(int) { // postfix --
        Checked_iter temp = *this;
        --*this;  // check by prefix
        return temp;
    }

    difference_type index() { return curr-c.begin(); } // random-access only
    Iter unchecked(){ return curr; }
};

void f2(list<int>& ls){

    int count = 0;
    try{
        Checked_iter< list<int> > p(ls, ls.begin() );
        while(true){
            ++p;
            ++count;
            //cout << "element: " << *p << "   count: " << count << endl;
        }
    }
    catch(out_of_bounds){
        cout << "overrun after " << count << " tries \n";
    }

}

void test9(){
    int a[] = {0,1,2,3,4,5,6,7,8,9};
    list<int> l(a, a+sizeof(a)/sizeof(int));
    show(l);
    f2(l);
}

最佳答案

Checked_iter 的构造函数应该采用 Cont&,而不是 Cont。您正在存储指向临时拷贝的指针。

关于c++ - 检查迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8176011/

相关文章:

c++ - 如何修复 "NO match found for ' 系统(字符串)

c++ - 如何在不打开 Qt 新窗口的情况下更改 UI?

c++ - 如何使用 bool 来防止在一个类中加载两次

c++ - boost::move 和 STL 算法

C++ While 循环和数组

c++ - QProgressDialog 栏在完成时重置为零

java - 集合与容器的区别

c++ - 具有调试功能的 C/C++ IDE 的建议

c++ - boost boost::iterator_facade 取消引用

Java HashMap 导致 ClassCastException