C ++ STL函数find()不接受用户定义类的迭代器参数

时间:2016-12-08 16:24:00

标签: c++ templates stl iterator

当我在std :: find()中传递用户定义的迭代器参数时,GCC 5.2.1。编译器(在Ubuntu 15.10上)给出了两条错误消息:

(1)

  

/ usr / include / c ++ / 5 / bits / stl_algo.h:162:34:错误:没有匹配   调用'__iterator_category(Text_iterator&)'的函数   的std :: __的iterator_category(__第一));

(2)

  

/ usr / include / c ++ / 5 / bits / stl_iterator_base_types.h:204:5:错误:否   在'struct std :: iterator_traits'

中输入名为'iterator_category'的类型

错误是由行auto p = find(first, last, first_char);引起的,它位于find_txt()函数内部。当该行被注释掉时,代码将无缝编译。以下是导致错误的代码摘录:

#include "std_lib_facilities.h"//from B. Stroustrup's site

using Line = vector<char>;

class Text_iterator {

    list<Line>::iterator ln; //points to lines
    Line::iterator pos; //points to characters

    public: 
    Text_iterator(list<Line>::iterator ll, Line::iterator pp)
        :ln{ll}, pos{pp} { }
    char& operator*() { return *pos; }
    Text_iterator& operator++();
    bool operator==(const Text_iterator& other) const
        { return ln==other.ln && pos==other.pos; }
    bool operator!=(const Text_iterator& other) const
        { return !(*this==other); }
};

Text_iterator& Text_iterator::operator++()
{
    ++pos;
    if (pos==(*ln).end()) {
    ++ln;
    pos = (*ln).begin();
}
    return *this;
}

Text_iterator find_txt(Text_iterator first, Text_iterator last, const string& s)
{
    if (s.size()==0) return last;// can’t find an empty stringchar first_char = s[0];
    char first_char = s[0];
    while (true) {
        auto p = find(first, last, first_char); //<------------the PROBLEM!!!!!!
        //if (p==last || match(p,last,s)) return p;
        //first = ++p;// look at the next character
    }
}

void ex6()
{
    ;
}


int main()
{
    ex6();
}

我提到了错误消息中提到的文件:

template<typename _Iterator, typename _Predicate>
    inline _Iterator
    __find_if(_Iterator __first, _Iterator __last, _Predicate __pred)
    {
        return __find_if(__first, __last, __pred,
        std::__iterator_category(__first)); //this is line #162 in stl_algo.h
    }

template<typename _Iter>
    inline typename iterator_traits<_Iter>::iterator_category
    __iterator_category(const _Iter&)//this is line #204 in stl_iterator_base_types.h
    { return typename iterator_traits<_Iter>::iterator_category(); }

问题出在auto p = find(first, last, first_char);或这两个GCC库文件中 - 即stl_algo.h和stl_iterator_base_types.h?有什么可能的方法来处理它?<​​/ p>

我正在为Stroustrup的编程:使用C ++的原理和实践,第二版编写第6章练习的代码。并被困在这里。在因特网上搜索std :: find()问题一直无济于事。没有任何问题涉及迭代器对此功能的论证。

1 个答案:

答案 0 :(得分:9)

标准算法(包括std::find)要求使用的迭代器满足Iterator概念的要求。其中的要求是

  

std::iterator_traits<It>有成员typedef value_typedifference_typereferencepointeriterator_category

根据错误消息,

  

'struct std :: iterator_traits'中没有名为'iterator_category'的类型

可能是指std::iterator_traits<Text_iterator>。因此,显然自定义迭代器不是迭代器,因为它不符合要求。

解决方案:为自定义迭代器类型专门化std::iterator_traits模板,并定义所需的成员类型。还要确保满足Iterator的其他要求以及InputIterator,因为这是std::find所需要的。

示例专业化:

namespace std {
    template<>
    struct iterator_traits<Text_iterator> {
        typedef ptrdiff_t          difference_type;
        typedef char               value_type;
        typedef char*              pointer;
        typedef char&              reference;
        typedef input_iterator_tag iterator_category;
    };
}