Range Adaptors
Range Adaptorsやべぇぇぇーーー!
http://www.boost.org/doc/libs/1_45_0/libs/range/doc/html/range/reference/adaptors/reference.html
先の日記のRange Adaptors版
vectorに格納されたクラスのメンバ関数を呼び出し、
戻り値の総和を得る。
#include <boost/range/numeric.hpp> #include <boost/range/adaptor/transformed.hpp> #include <boost/shared_ptr.hpp> #include <boost/make_shared.hpp> #include <boost/mem_fn.hpp> #include <iostream> #include <vector> class Dog { int age; public: explicit Dog(int age) :age(age){} int get_age()const{return age;} }; int main() { using namespace std; using namespace boost; using namespace boost::adaptors; vector< shared_ptr< Dog > > dogs; dogs.push_back( make_shared<Dog>(3) ); dogs.push_back( make_shared<Dog>(4) ); dogs.push_back( make_shared<Dog>(5) ); int sum_age = accumulate(dogs | transformed( mem_fn(&Dog::get_age) ) , 0); cout << sum_age << endl; }
うおおおおおおおすげーーーーー
もういっちょ。
mapに格納されたすべての要素のvalueを標準出力する。
#include <boost/range/algorithm.hpp> #include <boost/range/adaptor/map.hpp> #include <iostream> #include <map> int main() { using namespace std; using namespace boost::adaptors; map<int,string> m; m.insert( make_pair( 3,"san" ) ); m.insert( make_pair( 2,"ni" ) ); m.insert( make_pair( 5,"go" ) ); copy( m | map_values,ostream_iterator<string>(cout,"\n") ); }
うおおおおおおおすげーーーーーーーーーーーー