首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检查过的迭代器

检查过的迭代器
EN

Stack Overflow用户
提问于 2011-11-18 07:46:56
回答 1查看 1.2K关注 0票数 1

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

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

另外,我不明白为什么"operator“是这样实现的。顺便说一句,我使用的是VS2008。

代码语言:javascript
复制
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);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-19 01:35:43

Checked_iter的构造函数应该接受Cont&,而不是Cont。您正在存储指向临时副本的指针。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8176011

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档