当我在 std::find() 中传递用户定义的迭代器参数时,GCC 5.2.1.编译器(在 Ubuntu 15.10 上)给出两条错误消息:
(1)
(2)
该错误是由 find_txt() 函数内的 auto p = find(first, last, first_char);
行引起的。当该行被注释掉时,代码将无缝编译。这是导致错误的代码摘录:
#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?处理它的可能方法是什么?我正在为 Stroustrup 的编程:使用 C++ 的原则和实践,第 2 版的第 20 章练习 6 准备代码。并被困在这里。在 Internet 上搜索 std::find() 问题一直无济于事。没有一个问题涉及到这个函数的迭代器参数。
最佳答案
标准算法(包括 std::find
)要求使用的迭代器满足 Iterator 概念的要求。这些要求包括
根据错误信息,
这可能是指 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;
};
}
关于c++ - C++ STL 函数 find() 不接受用户定义类的迭代器参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41044165/