简体   繁体   English

STL 容器中的迭代器实现

[英]iterators implementation in STL containers

This is the sample code I've written, just a simple version to test out an iterator implementation:这是我编写的示例代码,只是测试迭代器实现的简单版本:

template <typename T>
struct ArrayIterator {
  using value_type = typename T::value_type;
  using pointer = value_type*;
  using reference = value_type&;
  using self = ArrayIterator;
  
  pointer ptr;
  
  ArrayIterator() : ptr{nullptr} {}
  ArrayIterator(pointer ptr) : ptr{ptr} {}
  
  reference operator*() {
    return *ptr;
  }
  
  bool operator==(self const& other) const {
    return ptr == other.ptr;
  }
  
  bool operator!=(self const& other) const {
    return !(*this == other);
  }
  
  self operator++() {
    ++ptr;
    return *this;
  }
  
  self operator++(int) {
    auto copy = *this;
    ++ptr;
    return copy;
  }
  
};

template <typename T, size_t size>
struct Array {
  T arr[size];
  
  using value_type = T;
  using iterator = ArrayIterator<Array<T, size>>;
  
  iterator begin() {
    return iterator(arr);
  }
  
  Array() : arr{1, 3, 4, 22, 3} {}
  
  iterator end() {
    return iterator(arr + size);
  }
};

int main() {
  using array = Array<int, 5>;
  array arr;
}

Assuming everything is public, and we only pass int as type parameter, and the array has the default constructor just to test range-based for loop.假设一切都是公开的,我们只传递int作为类型参数,并且数组具有默认构造函数只是为了测试基于范围的for循环。 It works.有用。

My question is, why can't we use inheritance?我的问题是,为什么我们不能使用 inheritance? Something like this:像这样的东西:

template <typename T, size_t size>
struct Array : ArrayIterator<Array<T, size>> {}

If used, I get the following error (compiler is clang 11):如果使用,我得到以下错误(编译器是 clang 11):

first.cpp:46:34: error: no type named 'value_type' in 'Array<int, 5>'
  using value_type = typename T::value_type;
                     ~~~~~~~~~~~~^~~~~~~~~~
first.cpp:82:16: note: in instantiation of template class 'ArrayIterator<Array<int, 5>>' requested here
struct Array : ArrayIterator<Array<T, size>> {
               ^
first.cpp:101:9: note: in instantiation of template class 'Array<int, 5>' requested here
  array arr;
        ^
1 error generated

I can't understand what's going on.我不明白发生了什么事。

You can't use inheritance like you want because the code will have a catch-22 if you try.您不能像您想要的那样使用 inheritance,因为如果您尝试,代码将有一个 catch-22。 ArrayIterator will try to define its value_type as an alias of the array's value_type before the latter has been defined yet. ArrayIterator将尝试将其value_type定义为数组value_type的别名,然后再定义后者。 That is why you are getting the compiler error.这就是您收到编译器错误的原因。

Your iterator has no need to access the Array class itself, so there is no point in passing the Array class type to the iterator's template parameter.您的迭代器不需要访问Array class 本身,因此将Array class 类型传递给迭代器的模板参数是没有意义的。 Just pass the array's value_type instead of Array itself, and change the iterator's value_type to be just T instead of typename T::value_type :只需传递数组的value_type而不是Array本身,并将迭代器的value_type更改为T而不是typename T::value_type

template <typename T>
struct ArrayIterator {
  using value_type = T;
  using pointer = value_type*;
  using reference = value_type&;
  using self = ArrayIterator;
  
  pointer ptr;
  
  ArrayIterator() : ptr{nullptr} {}
  ArrayIterator(pointer ptr) : ptr{ptr} {}
  
  reference operator*() {
    return *ptr;
  }
  
  bool operator==(self const& other) const {
    return ptr == other.ptr;
  }
  
  bool operator!=(self const& other) const {
    return ptr != other.ptr;
  }
  
  self& operator++() {
    ++ptr;
    return *this;
  }
  
  self operator++(int) {
    auto copy = *this;
    ++ptr;
    return copy;
  }
};

template <typename T, size_t size>
struct Array {
  using value_type = T;
  using iterator = ArrayIterator<T>;

  T arr[size];

  iterator begin() {
    return iterator(arr);
  }
  
  iterator end() {
    return iterator(arr + size);
  }
};

int main() {
  using array = Array<int, 5>;
  array arr{1, 3, 4, 22, 3};
  for (auto val : arr) {
    cout << val << endl;
  }
}

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

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