简体   繁体   English

简单的迭代器:std :: count错误

[英]Simple iterator: error with std::count

this is a simple exercise. 这是一个简单的练习。 I'd like to write a custom iterator for a C-style array of characters all by myself: no boost. 我想为我自己编写一个C样式字符数组的自定义迭代器:没有提升。 I think I'm halfway through, but I get an error when using std::count . 我想我已经过了一半,但是使用std::count时出现错误。

My iterator is this: 我的迭代器是这样的:

class myIterator : std::iterator<std::input_iterator_tag, char>
{
    char *p;

public:

    // Definitions
    typedef typename std::iterator<std::input_iterator_tag, char>::difference_type difference_type;
    typedef typename std::iterator<std::input_iterator_tag, char>::value_type value_type;
    typedef typename std::iterator<std::input_iterator_tag, char>::reference reference;
    typedef typename std::iterator<std::input_iterator_tag, char>::pointer pointer;

    // Implementation
    myIterator(char* x) :p(x) { };
    myIterator(const myIterator& i) : p(i.p) { };
    myIterator& operator++() { ++p; return *this; };
    myIterator operator++(int) {myIterator tmp(*this); operator++(); return tmp; };
    bool operator==(const myIterator& rhs) { return p == rhs.p; };
    bool operator!=(const myIterator& rhs) { return p != rhs.p; };
    char& operator*() { return *p; };
};

So, when I use the iterator in a for loop, it's great, it 因此,当我在for循环中使用迭代器时,它很棒

for (auto it = data.begin(); it != data.end(); it++)
   std::cout << *it;

However, this gets a compile-time error: 但是,这会出现编译时错误:

std::cout << std::count(data.begin(), data.end(), '@') << std::endl;

For what it's worth, data is a super-simple class with a classic wrapping function for begin() and end() ( ie , return myIterator(address_); and return myIterator(address_ + size_); ). 就其价值而言, data是一个非常简单的类,具有用于begin()end()的经典包装函数( return myIterator(address_);return myIterator(address_ + size_); )。

What's the error is puzzling to me: 错误是什么让我感到困惑:

error: no matching function for call to 'count'
[...]
note: candidate template ignored: substitution failure [with _InputIterator = myIterator, _Tp = char]: no type named 'difference_type' in 'std::__1::iterator_traits<myIterator>'
count(_InputIterator __first, _InputIterator __last, const _Tp& __value_)

I am missing something here: I declared difference_type in the iterator. 我在这里缺少了一些东西:我在迭代器中声明了difference_type

Can anyone help? 有人可以帮忙吗?

Deriving your class from std::iterator already defines value_type , difference_type , etc. So you shouldn't be defining them. std::iterator派生的类已经定义了value_typedifference_type等。因此,您不应该定义它们。 Just make your inheritance public and remove those typedef s and it will work: 只需公开您的继承并删除那些typedef ,它将起作用:

class myIterator : public std::iterator<std::input_iterator_tag, char>
{
    char *p;

public:
    // Implementation
    myIterator(char* x) :p(x) { };
    // ......
};

Note that it's not necessary to remove the typedef s, but there is no reason to define them manually, std::iterator already does that. 注意,不必删除typedef ,但是没有理由手动定义它们, std::iterator已经做到了。

Demo here . 演示在这里

I'm not sure if this is the problem, but the usual way of using std::iterator is to inherit publicly from it. 我不确定这是否是问题,但是使用std::iterator的通常方法是从中公开继承。 Or to not use it, and define all five of its typedefs. 或者不使用它,并定义所有五个 typedef。 I'd guess the reason your code doesn't work is that the typedef for iterator_category isn't accessible: if you don't derive publicly from std::iterator , you have to provide it. 我想您的代码无法正常工作的原因是iterator_category的typedef无法访问:如果您不公开从std::iterator派生,则必须提供它。

For what it's worth, there are three ways of making the iterator type conform: inherit publicly from std::iterator , declare the five typedefs ( value_type , difference_type , pointer , reference and iterator_category ) directly in your class (as public), or specialize std::iterator_traits on your class (again, with the five typedefs). 对于它的价值,也有使迭代式的三种方式符合:从公开继承std::iterator ,声明的五个类型定义( value_typedifference_typepointerreferenceiterator_category )直接在你的类(公共),或专门类上的std::iterator_traits (同样,使用五个typedef)。 The first is by far the simplest and the most usual. 到目前为止,第一个是最简单,最常用的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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