boost::tuple
#include <assert.h> #include <boost/tuple/tuple.hpp> int main(){ using boost::tuple; namespace tuples = boost::tuples; tuple<int, float, char> hoge(20, 3.14f, 'w'); assert( hoge.get<0>() == 20 ); assert( hoge.get<1>() == 3.14f ); assert( hoge.get<2>() == 'w' ); typedef tuple<int, float, double> my_tuple; assert(tuples::length<my_tuple>::value == 3); return 0; }
テンプレートで型を指定して、複数の値を保持することが出来るクラス。
std::pair の3個以上版だと思えば分かりやすいか?
それぞれの値には、get
また、保持している値の数は、boost::tuples::length
#include <assert.h> #include <boost/tuple/tuple.hpp> boost::tuple<int, int> division(int a, int b){ return boost::make_tuple(a / b, a % b); } int main(){ int div, mod; boost::tie(div, mod) = division(13, 5); assert(div == 2); assert(mod == 3); return 0; }
こんな感じに使えば複数の値を返すことが可能に。
boost::tie を使用すれば分割して値を取得する事できます。
ちなみに値を保持できる数は10個まで。
[boost]
ver 1.44.0
[参照]
http://www.kmonos.net/alang/boost/classes/tuple.html
http://www.boost.org/doc/libs/1_44_0/libs/tuple/doc/tuple_users_guide.html#tiers