asker
asker

Reputation: 298

How to make an iterable class in C++?

I have a linked list code:

#include <iostream>
class Node
{
private:
    /* data */
public:
    int value;
    Node* next;
    Node(int value){
        this->value = value;
        this->next = nullptr;
    }
};

class LinkedList
{
private:
    /* data */
public:
    Node *start;

    LinkedList(){
        this->start = nullptr;
    }

    void insert(int value){
        Node *node = new Node(value);
        if (this->start == nullptr)
        {
            this->start = node; 
        }else
        {
            Node* temp = this->start;
            while (temp->next != nullptr)
            {
                temp = temp->next;
            }
            temp->next = node;
        }
        
        

    }

    void print(){
        Node* temp = this->start;
        while (temp != nullptr)
        {
            std::cout<<temp->value<<std::endl;
            temp = temp->next;
        }
        
    }

    void __iter__(){
        Node* node = this->start;
        while (node)
        {
            yield node;
            node = node->next;
        }
        
    }
};

int main(int argc, char const *argv[])
{
    LinkedList listed;
    listed.insert(4);
    listed.insert(7);
    listed.insert(9);
    listed.insert(6);
    listed.print();

    return 0;
}

As you can see I have __iter__ method in my LinkedList class, but I have found that yield is not define to use in C++, so my console just shows:

info.cpp: In member function 'void LinkedList::__iter__()':
info.cpp:59:13: error: 'yield' was not declared in this scope
   59 |             yield node;
      |             ^~~~~

I am taking as reference the next piece of python code:

def __iter__(self):
        node = self.start
        while node:
            yield node
            node = node.next

I hope you can help me to solve that, thanks.

Upvotes: 2

Views: 394

Answers (1)

Lajos Arpad
Lajos Arpad

Reputation: 77045

Take a look at this source: https://en.cppreference.com/w/cpp/iterator/iterator

It says that

std::iterator is the base class provided to simplify definitions of the required types for iterators.

Furthermore:

enter image description here

This source https://cplusplus.com/reference/iterator/iterator/

gives you the following example:

// std::iterator example
#include <iostream>     // std::cout
#include <iterator>     // std::iterator, std::input_iterator_tag

class MyIterator : public std::iterator<std::input_iterator_tag, int>
{
  int* p;
public:
  MyIterator(int* x) :p(x) {}
  MyIterator(const MyIterator& mit) : p(mit.p) {}
  MyIterator& operator++() {++p;return *this;}
  MyIterator operator++(int) {MyIterator tmp(*this); operator++(); return tmp;}
  bool operator==(const MyIterator& rhs) const {return p==rhs.p;}
  bool operator!=(const MyIterator& rhs) const {return p!=rhs.p;}
  int& operator*() {return *p;}
};

int main () {
  int numbers[]={10,20,30,40,50};
  MyIterator from(numbers);
  MyIterator until(numbers+5);
  for (MyIterator it=from; it!=until; it++)
    std::cout << *it << ' ';
  std::cout << '\n';

  return 0;
}

It includes the iterator via #include <iterator>, defines its operations as public and then inside the main function effectively uses it. You will need to apply something similar, but you will need to step between pointers for your operator++ methods.

Upvotes: 2

Related Questions