Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheriting std::vector::iterator for custom class?

I am implementing a custom class that contains an STL std::vector as central data member. Now, I would like this class to provide an iterator, which merely needs to iterate through this vector and also works with C++11 range based iteration. It is very tempting to just somehow inherit the iterator from std::vector::iterator as it is supposed to do exactly the same job. Is this possible or do I need to implement a completely custom iterator?

class Custom {
private:
  std::vector<double> _data;
public:
  class iterator {
    // Want this to provide an interface to iterate through _data
    // ...
  };
  // ...
};

Custom C;
// Populate C with data ...
for (const auto& item : C) {
  // This should print the elements within _data.
  std::cout << item << std::endl;
}
like image 962
jhansen Avatar asked Oct 19 '22 12:10

jhansen


1 Answers

You don't need to inherit from the iterator itself. You can just provide interfaces for the iterators used by the std::vector<double>.

Here is a quick snippet:

#include <vector>
#include <iostream>

class Custom {
private:
  std::vector<double> _data;
public:
  explicit Custom(std::initializer_list<double> init) : _data(init) {}

  using iterator = std::vector<double>::iterator;
  using const_iterator = std::vector<double>::const_iterator;

  iterator begin()
  {
    return _data.begin();
  } 

  iterator end()
  {
    return _data.end();
  } 

  const_iterator cbegin() const
  {
    return _data.cbegin();
  } 

  const_iterator cend() const
  {
    return _data.cend();
  } 
};

int main()
{
  Custom C({ 1.0,2.0,3.0,4.0,5.0 });
  for (const auto &item : C)
  {
    std::cout << item << "\n";
  } 

  return 0;
}
like image 157
Lukas Avatar answered Nov 15 '22 13:11

Lukas