<boost/foreach.hpp> 提供两个宏:BOOST_FOREACH 和 BOOST_REVERSE_FOREACH
#include#include #include #include int main(){ std::vector vect = boost::assign::list_of(1)(2)(3); BOOST_FOREACH(int x,vect) //BOOST_FOREACH 与 BOOST_REVERSE_FOREACH 都是宏 { std::cout< <<","; } std::string str = "Hello"; std::cout<<"\n"; BOOST_REVERSE_FOREACH(char& c,str) { std::cout< <<","; } std::cout<<"\n"; BOOST_FOREACH(BOOST_TYPEOF(*str.begin()) c,str) //配合 BOOST_TYPEOF 来使用匿名类型 { std::cout< <<","; }}