迭代MAP
map<int,int> mi;
for (int x=0;x<10;x++)
{
mi.insert(make_pair(x,x+1));
}
typedef map<int,int> m;
BOOST_FOREACH(m::value_type& i,mi)
{
cout<<i.second<<endl;
}
迭代字符串
std::string hello( "Hello, world!" );
BOOST_FOREACH( char ch, hello )
{
std::cout << ch;
}
迭代vertor中的vertor
std::vector<std::vector<int> > matrix_int; BOOST_FOREACH( std::vector<int> & row, matrix_int ) BOOST_FOREACH( int & i, row ) ++i;
迭代list
std::list<int> list_int( /*...*/ ); BOOST_FOREACH( int i, list_int ) { // 用 i 执行某些操作
}
迭代数组
short array_short[] = {1,2,3}; BOOST_FOREACH( int i, array_short ) { // short 被隐式转换为 int
}让
BOOST_FOREACH
更漂亮#define foreach BOOST_FOREACH
#define reverse_foreach BOOST_REVERSE_FOREACH