7

Concept

So, I'm trying to define a macro to simplify the following code:

for (vector<TYPE>::iterator iter = iterable.begin(); 
             iter != iterable.end(); iter++)

and

for (map<TYPE, TYPE>::iterator iter = iterable.begin();
             iter != iterable.end(); iter++)

etc.

Existing Work

So far, I've got

#define every(iter, iterable) ::iterator iter = iterable.begin(); iter != iterable.end(); iter++
for (vector<TYPE> every(iter, iterable))

but I'd like to simplify this further.

Goal

Ideally, I'd like to be able to do

for (every(iter, iterable))

which means that I'd need to somehow get the class<TYPE> of the iterable object. Is this possible? If so, how can I do it?

Stipulations

  • This, ideally, needs to go into a (relatively) large codebase already set up to access the iterator object.
  • I am running on a compiler pre - C++11

Victory

#define every(iter, iterable) typeof(iterable.begin()) iter = iterable.begin(); iter != iterable.end(); iter++
for (every(iter, iterable))
1
  • 1
    Can you use C++11 and decltype? If not, you can write a small trait class.
    – Kerrek SB
    Dec 9, 2011 at 1:23

6 Answers 6

8

This answer does not depend on C++11, but it needs typeof, which some compilers may not have. Should work with any recent g++

#define For(iter, iterable) for(typeof((iterable).begin()) iter = (iterable).begin(); iter != (iterable).end(); ++iter)
5
  • it's so straightforward, i missed it! Dec 9, 2011 at 1:36
  • 1
    I did not know typeof could be used this way. +1 Dec 9, 2011 at 1:37
  • I think there are some limitations to typeof. If f() returns a reference type, such as int&, then (as far as I remember) typeof(f()) is int, not int&. But it does a good job in the macro I just described. Dec 9, 2011 at 1:39
  • Nice solution, although Ross' for( auto& x : v ) would have my preference if the compiler supports it (i.e. you are using C++11).
    – CompuChip
    Mar 11, 2015 at 15:35
  • You can now also use auto instead of typeof for this.
    – Pan P
    Apr 17, 2019 at 13:13
2

You can use for(auto iter = iterable.being(); iter != iterable.end(); iter++) if your compiler supports C++0x.

1
  • In C++0x one could use range-for instead. Also ++iter is preferred in C++. ;)
    – TrueY
    Jun 11, 2015 at 13:03
2

If you are using C++11, you may use the new for syntax.

vector<double> v(9, 0.5);
auto total = 0.;
for (auto x: v) {
    total += x;
}

If you need a reference to modify the values, you may use:

vector<double> v(9, 0.5);
for (auto &x: v) {
    x = 5;
}

Just compile with the flag -std=c++0x.

1

If you're using c++11 then use auto!

for (auto it = vec.begin(); it != vec.end(); it++)
{
}

edit:

This would be your macro:

#define every(iter, iterable) auto iter = iterable.begin(); iter != iterable.end(); iter++

Then the implmentation:

for(every(iter, iterable))
{
    UseElement(*iter);
}
4
  • auto doesn't return an iterator properly (or at least, when I use auto, I get errors saying things like "no match for operator++ in TYPE") Dec 9, 2011 at 1:16
  • it's identical. i'm under the impression that auto is doing something different, as i'm compiling with a pre - C++11 compiler Dec 9, 2011 at 1:31
  • Ahh yeah it's a C++11 feature so that part would be important :) Dec 9, 2011 at 1:39
  • In C++11 there wouldn't be any need for this macro. (Ideally, currently auto might be supported, but range-for not.)
    – UncleBens
    Dec 9, 2011 at 15:12
0

Why not simply use std::for_each ? In C++11 you can use them with lambdas.

C++11 also has a range base loop.

template<typename T>
void Foo(const T& x)
{
    for (auto& i : x)
        std::cout << i << std::endl;
}

Of course the obligatory...MACROS ARE EVIL

If you are stuck with an ancient compiler you can always do

typedef std::map<int,int> IntMap_t;
IntMap_t tmap;
for( IntMap_t::iterator iter = tmap.begin();
         iter != tmap.end();
         ++iter)
{
}
1
  • if i could assume that iterable would be a map<int, int>, couldn't i just include that in the macro? unless map<int, int> is some sort of weird "universal iterable" in c++ Dec 9, 2011 at 1:21
0

I would like to extend Aaron McDaid's pretty nice answer. I had found very similar solution in pre-C++0x environment (I called the macro as FOREACH). It works for normal iterator and reverse iterator. But how could it be done on const_iterator and const_reverse_iterator? Unfortunately cbegin() is a C++0x feature, and typeof(container)::const_iterator does not work. A very small template class can solve the issue. Here is my code:

template <class T>
struct IterType { typedef typename T::const_iterator citer_t; };

#define FOREACHCONST(_CIter, _Container) \
  for(IterType<typeof(_Container)>::citer_t _CIter = (_Container).begin(); \
      _CIter != (_Container).end(); ++_CIter )

It works exactly on the same way as FOREACH.

Interestingly this does not work inside a templated function. In that case the typename keyword has to be added right before IterType, but in that case it cannot used outside a templated function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.