Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Iterable-like behavior in C++ attainable?

Tags:

c++

Java allows classes to reveal Iterable types so clients can traverse some instance's collection of data, like so:

public class MyClass
{
    private ArrayList<String> strings;
    private ArrayList<Integers> ints;
    public MyClass() { /* generate data ... */ }
    public Iterable<String> allStrings() {return strings;}
    public Iterable<Integer> allInts() {return ints;}
}

This has always struck me as "clean" because it maintains encapsulation, allowing me to change the ArrayLists to LinkedLists if I wanted to and is still convenient to the client in constructs such as for(String s : myClassInstance.allStrings()) //....

In C++, however, if I want to allow the client to use my for-loop, in absence of an Iterable, I need to return a const vector<T>& or whatever, which is obviously not too great.

Defining template<> begin<my_class> {/*...*/} and friends is nice, but only if my_class has one collection to iterate over. What else can I do?

like image 605
VF1 Avatar asked Apr 29 '14 00:04

VF1


People also ask

What is difference between iterable and iterator?

An Iterable is basically an object that any user can iterate over. An Iterator is also an object that helps a user in iterating over another object (that is iterable). We can generate an iterator when we pass the object to the iter() method. We use the __next__() method for iterating.

What is iterable type?

Iterable is a pseudo-type introduced in PHP 7.1. It accepts any array or object implementing the Traversable interface. Both of these types are iterable using foreach and can be used with yield from within a generator.

What is CPP iterable?

C++ has iterators, although they work slightly differently than in Java. In fact the Iterable<T> interface is just a layer that allows the Java compiler to use some syntactic sugar to automate the actual iteration.

What is the meaning of iterable?

Iterable is an object which can be looped over or iterated over with the help of a for loop. Objects like lists, tuples, sets, dictionaries, strings, etc. are called iterables. In short and simpler terms, iterable is anything that you can loop over.


1 Answers

just make Iterable in C++

template<class T, class U>
struct Iterable
{
    T _begin;
    U _end;

    Iterable(T begin, U end)
    : _begin(begin), _end(end)
    {}

    T begin()
    {
        return _begin;
    }

    U end()
    {
        return _end;
    }
};

template<class T, class U>
Iterable<T,U> make_iterable(T t, U u)
{
    return Iterable<T,U>(t, u);
}

struct MyClass 
{
    std::vector<int> _ints;
    std::vector<std::string> _strings;

    auto allInts() -> decltype(make_iterable(_ints.begin(), _ints.end()))
    {
        return make_iterable(_ints.begin(), _ints.end());
    }

    auto allStrings() -> decltype(make_iterable(_strings.begin(), _strings.end()))
    {
        return make_iterable(_strings.begin(), _strings.end());
    }
};

then use it like

for (auto i : foo.allInts())
{
    cout << i << endl;
}

for (auto i : foo.allStrings())
{
    cout << i << endl;
}

live example

like image 85
Bryan Chen Avatar answered Oct 14 '22 10:10

Bryan Chen