C ++和语言扩展中的Foreach语句?

时间:2021-03-16 22:30:52

You can simulate foreach-statement in c++ with macro declaration. I'm using similar syntax for looping arrays in the following way:

您可以使用宏声明在c ++中模拟foreach语句。我使用类似的语法以下列方式循环数组:

int array1[10];
vector<int> array2(10);

fori(array1)
    forj(array2)
        fork(123)
             if(array1[i]==array[j])
                  return true;

What's your favorite macros for extending c++ language in some way?

你最喜欢的以某种方式扩展c ++语言的宏是什么?

EDIT:

Implementation of the macro mentioned in the question is:

问题中提到的宏的实施是:

#define fori(a) for(int i=0;i<getsize(a);i++)
#define forj(a) for(int j=0;j<getsize(a);j++)
#define foru(a) for(int u=0;u<getsize(a);u++)
#define fork(a) for(int k=0;k<getsize(a);k++)
#define forl(a) for(int l=0;l<getsize(a);l++)

template<typename T>
int getsize(T& v ){return v.size();}
template<typename T,int N>
int getsize(T(&v)[N]){return N;}
int getsize(int v){return v;}

3 个解决方案

#1


You should check out Boost.Foreach.

你应该看看Boost.Foreach。

vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
BOOST_FOREACH(int& i, v)
{
    i = i * i;
}

#2


If for some reason I need to skip STL containers I use this macro for my container classes:

如果由于某种原因我需要跳过STL容器,我将这个宏用于我的容器类:

#define M_foreach( iterable ) \
do {\
  (iterable).prepareit();\
  while (  (iterable).stepit()  ) {

#if !defined( M_end )
#  define M_end   } } while( 0 );
#endif

(that macro assumes that you use a container that has an iteration interface)

(该宏假定您使用具有迭代接口的容器)

Container foo;
M_foreach ( foo )
   ...
   Container bar;
   M_foreach ( bar )
      ...
   M_end
   ...
M_end

#3


I find boost's macro a bit cumbersome.

我发现boost的宏有点麻烦。

Here's my attempt:

这是我的尝试:

#include <boost/typeof/typeof.hpp>
#define FOR_EACH(cont, iter)    \   
  for (BOOST_TYPEOF(cont)::const_iterator iter = cont.begin(), e = cont.end(); \
    iter != e; \
    ++iter)

Works for standard containers.

适用于标准容器。

Used like so:

像这样使用:

vector<int> vi;
// ...
FOR_EACH(vi, i)
  cout << *i;

Supports nesting:

map<string, list<int> > msli;
msli["key1"].push_back(1);
msli["key1"].push_back(2);
msli["key2"].push_back(3);
msli["key2"].push_back(4);

FOR_EACH(msli, i) {
  cout << i->first << endl;
  FOR_EACH(i->second, j)
    cout << "\t" << *j << endl;
}

/*
Prints:
  key1
    1
    2
  key2
    3
    4
*/

Source: http:/wtw.tw/papers/foreach.html

#1


You should check out Boost.Foreach.

你应该看看Boost.Foreach。

vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
BOOST_FOREACH(int& i, v)
{
    i = i * i;
}

#2


If for some reason I need to skip STL containers I use this macro for my container classes:

如果由于某种原因我需要跳过STL容器,我将这个宏用于我的容器类:

#define M_foreach( iterable ) \
do {\
  (iterable).prepareit();\
  while (  (iterable).stepit()  ) {

#if !defined( M_end )
#  define M_end   } } while( 0 );
#endif

(that macro assumes that you use a container that has an iteration interface)

(该宏假定您使用具有迭代接口的容器)

Container foo;
M_foreach ( foo )
   ...
   Container bar;
   M_foreach ( bar )
      ...
   M_end
   ...
M_end

#3


I find boost's macro a bit cumbersome.

我发现boost的宏有点麻烦。

Here's my attempt:

这是我的尝试:

#include <boost/typeof/typeof.hpp>
#define FOR_EACH(cont, iter)    \   
  for (BOOST_TYPEOF(cont)::const_iterator iter = cont.begin(), e = cont.end(); \
    iter != e; \
    ++iter)

Works for standard containers.

适用于标准容器。

Used like so:

像这样使用:

vector<int> vi;
// ...
FOR_EACH(vi, i)
  cout << *i;

Supports nesting:

map<string, list<int> > msli;
msli["key1"].push_back(1);
msli["key1"].push_back(2);
msli["key2"].push_back(3);
msli["key2"].push_back(4);

FOR_EACH(msli, i) {
  cout << i->first << endl;
  FOR_EACH(i->second, j)
    cout << "\t" << *j << endl;
}

/*
Prints:
  key1
    1
    2
  key2
    3
    4
*/

Source: http:/wtw.tw/papers/foreach.html