MIRA
Namespaces | Macros
Foreach.h File Reference

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

#include <boost/foreach.hpp>
#include <boost/version.hpp>
#include <boost/typeof/typeof.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.

Namespaces

 boost
 

Macros

#define foreach   BOOST_FOREACH
 This macro provides a more convenient name for BOOST_FOREACH. More...
 
#define foreach_reverse   BOOST_REVERSE_FOREACH
 This macro provides a more convenient name for BOOST_REVERSE_FOREACH. 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
Date
2010/05/20

Macro Definition Documentation

◆ foreach

#define foreach   BOOST_FOREACH

This macro provides a more convenient name for BOOST_FOREACH.

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   BOOST_REVERSE_FOREACH

This macro provides a more convenient name for BOOST_REVERSE_FOREACH.

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)