MIRA
Macros
Foreach.h File Reference

Macro for iterating over all elements in a container. More...

#include <boost/range/adaptor/reversed.hpp>
Include dependency graph for Foreach.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define foreach(i, c)   for (i : c)
 The macro allows a PERL-or JAVA-like "foreach" construct that automates the iteration over containers or other sequences that provide iterator concepts. More...
 
#define foreach_reverse(i, c)   for (i : boost::adaptors::reverse(c))
 The macro can be used similar to foreach, but the containers are traversed in reverse order. More...
 
#define foreachIt(var, container)   for (auto var = container.begin(); var != container.end(); ++var)
 This macro provides a more convenient way for iterating over containers or other sequences that provide iterator concepts. More...
 

Detailed Description

Macro for iterating over all elements in a container.

Author
Erik Einhorn, Tom Mehner
Date
2010/05/20

Macro Definition Documentation

◆ foreach

#define foreach (   i,
 
)    for (i : c)

The macro allows a PERL-or JAVA-like "foreach" construct that automates the iteration over containers or other sequences that provide iterator concepts.

Examples:

std::list<int> list_int;
...
foreach(int i, list_int ) // i is accessed by value here
{
// do something with i
}
foreach(const int& i, list_int ) // i is accessed by const reference
{ // here and avoids unnecessary copying.
// do something with i
}
foreach(int& i, list_int ) // i is accessed by reference here
{
i = ... // change the value in the container
}
// the same also works for C-strings and std::strings:
std::string str( "Hello, world!" );
foreach(char ch, str)
{
std::cout << ch;
}

Supported sequence types:

  • STL containers
  • arrays
  • Null-terminated strings (char and wchar_t)
  • std::pair of iterators

For more details see: http://www.boost.org/doc/libs/1_43_0/doc/html/foreach.html

◆ foreach_reverse

#define foreach_reverse (   i,
 
)    for (i : boost::adaptors::reverse(c))

The macro can be used similar to foreach, but the containers are traversed in reverse order.

For more details see: http://www.boost.org/doc/libs/1_43_0/doc/html/foreach.html

◆ foreachIt

#define foreachIt (   var,
  container 
)    for (auto var = container.begin(); var != container.end(); ++var)

This macro provides a more convenient way for iterating over containers or other sequences that provide iterator concepts.

In contrast to the foreach macros above, this macro provides an iterator instead of the iterated value.

Examples:

std::list<int> list_int;
...
foreachIt(it, list_int )
{
int val = *it;
}

In fact, this macro is a simple replacement for

for(auto var=container.begin();var!=container.end();++var)