Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the for each loop function in C++ work with a custom class

I'm new to C/C++ programming, but I've been programming in C# for 1.5 years now. I like C# and I like the List class, so I thought about making a List class in C++ as an exercise.

List<int> ls; int whatever = 123; ls.Add(1); ls.Add(235445); ls.Add(whatever); 

The implementation is similar to any Array List class out there. I have a T* vector member where I store the items, and when this storage is about to be completely filled, I resize it.

Please notice that this is not to be used in production, this is only an exercise. I'm well aware of vector<T> and friends.

Now I want to loop through the items of my list. I don't like to use for(int i=0;i<n; i==). I typed for in the visual studio, awaited for Intellisense, and it suggested me this:

for each (object var in collection_to_loop) {  }         

This obviously won't work with my List implementation. I figured I could do some macro magic, but this feels like a huge hack. Actually, what bothers me the most is passing the type like that:

#define foreach(type, var, list)\ int _i_ = 0;\ ##type var;\ for (_i_ = 0, var=list[_i_]; _i_<list.Length();_i_++,var=list[_i_])   foreach(int,i,ls){     doWork(i); } 

My question is: is there a way to make this custom List class work with a foreach-like loop?

like image 455
Ricardo Pieper Avatar asked May 12 '13 04:05

Ricardo Pieper


People also ask

Can you do a for each loop in C?

There is no foreach in C. You can use a for loop to loop through the data but the length needs to be know or the data needs to be terminated by a know value (eg. null).

Are iterators used in for each loops C++?

Working of the foreach loop in C++ So basically a for-each loop iterates over the elements of arrays, vectors, or any other data sets. It assigns the value of the current element to the variable iterator declared inside the loop.

When did C++ get foreach?

In C++, it was introduced in C++ 11 and Java in JDK 1.5. 0 The keyword used for foreach loop is “for” in both C++ and Java.


1 Answers

Firstly, the syntax of a for-each loop in C++ is different from C# (it's also called a range based for loop. It has the form:

for(<type> <name> : <collection>) { ... } 

So for example, with an std::vector<int> vec, it would be something like:

for(int i : vec) { ... } 

Under the covers, this effectively uses the begin() and end() member functions, which return iterators. Hence, to allow your custom class to utilize a for-each loop, you need to provide a begin() and an end() function. These are generally overloaded, returning either an iterator or a const_iterator. Implementing iterators can be tricky, although with a vector-like class it's not too hard.

template <typename T> struct List {     T* store;     std::size_t size;     typedef T* iterator;     typedef const T* const_iterator;      ....      iterator begin() { return &store[0]; }     const_iterator begin() const { return &store[0]; }     iterator end() { return &store[size]; }     const_iterator end() const { return &store[size]; }      ...  }; 

With these implemented, you can utilize a range based loop as above.

like image 97
Yuushi Avatar answered Sep 22 '22 23:09

Yuushi