boost::BOOST_FOREACH
#include "stdafx.h" #include #include #include "boost/assign.hpp" #include "boost/bind.hpp" #include "boost/typeof/typeof.hpp" #include "boost/assert.hpp" #include #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP() #define foreach BOOST_FOREACH #define auto_t BOOST_AUTO #define assert_t BOOST_ASSERT using namespace boost::assign; using namespace std; #define BOOST_DISABLE_ASSERTS struct device { device( long LL,string SS,double DD) :m_id (LL) ,m_name (SS) ,m_double (DD) {} long m_id; string m_name; double m_double; }; BOOST_TYPEOF_REGISTER_TYPE(device) //注册一个自定义类 (貌似不注册也行) int main(int argc, _TCHAR* argv[]) { //超简洁的循环,且同 const_iterator 同样效果 std::vector > g4(3); foreach( const std::vector& row, g4) { //row.push_back(12); foreach( const int & i,row) { //todo } } //对于map 要换成 pair,但不知道为什么不能放到foreach括号中 std::map M = map_list_of(1,"11") (2,"22") (3,"33"); typedef const pair ff; foreach( ff& f,M) { cout << f.second ; } //对于自定义类型 std::vector G; device g1(11,"table",2.4); G.push_back(g1); foreach(const device & g6,G) { cout << g6.m_name ; } //没有找到实现 const 的办法,待解决 for ( auto_t( &gg,G.begin()); gg != G.end(); ++gg) { cout << gg->m_name ; } map > vg; vg.insert(make_pair("name",G)); for ( auto_t( &gg, vg.begin()); gg != vg.end(); ++gg) { cout << gg->first ; } set s = list_of(10)(20)(30); BOOST_TYPEOF(*s.begin()) y; //也可以用BOOST_AUTO(y, *s.begin()); foreach(y, s) { } return 0; }