boost::tuple
/*
1)Boost::tuple就为我们提供了一种类似于匿名struct的方法为我们解决函数的多个返回值的问题。
2)对boost::tuple你可以绑定10个参数,或者你可以迭代实现无限多参数的情况。
3)t.get<N>()或get<N>(t) ,取得第N个值
4)make_tuple ,生成tuple
5)tie , 生成都是ref的tuple
6) 重载比较运算符 ,可以直接用来比较
7)重载输入输出运算符 ,可以直接使用IO
8)get_head()和get_tail()函数,用来取得值
9)length<>和element<>用来得到tuple的size和第N个的值类型
10)如果使用boost::TR1,则还可以使用std::tr1::tuple_size(),std::tr1::tuple_element(),分别用来得到tuple的size和第N个值的类型。
*/
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include "boost/tuple/tuple.hpp"
#include "boost/tuple/tuple_comparison.hpp"
#include "boost/tuple/tuple_io.hpp"
#include "boost/bind.hpp"
#include "boost/array.hpp"
#include "boost/assign/list_of.hpp"
//Tuples 与 for_each
template <typename Function>
void for_each_element( const boost::tuples::null_type&, Function)
{}
template <typename Tuple, typename Function>
void for_each_element(Tuple& t, Function func)
{
func(t.get_head()); for_each_element(t.get_tail(),func);
}
struct print
{
template <typename T> void operator()(const T& t)
{ std::cout << t << '\n'; }
};
template <typename T> struct print_type
{
void operator()(const T& t)
{ std::cout << t << '\n'; }
template <typename U> void operator()(const U& u) {}
};
//虚函数
class base
{
public:
virtual ~base() {};
virtual void test()
{ std::cout << "父类::test()\n"; }
};
class derived : public base
{
public:
virtual void test()
{ std::cout << "子类::test()\n"; }
};
//数组排序
template <int Index> class element_less
{
public:
template <typename Tuple> bool operator()(const Tuple& lhs,const Tuple& rhs) const
{
return boost::get<Index>(lhs) < boost::get<Index>(rhs);
}
};
//函数返回多个值
boost::tuple<int, int> divide_and_modulo(int a, int b)
{
return boost::make_tuple<int, int> (a/b, a%b);
}
int main(int argc, _TCHAR* argv[])
{
//虚函数
boost::tuple<int, std::string,derived> tup1(-5,"Tuples");
boost::tuple<unsigned int,std::string,base > tup2;
tup2=tup1;
tup2.get<2>().test();
tup1.get<2>().test();
std::cout << "Interesting value: " << tup2.get<1>() << '\n';
//定义一个tuple
typedef boost::tuple<short,int,long> Mtuple;
std::vector<Mtuple> vec ;
vec= boost::assign::tuple_list_of(3,2,7)(6,2)(7,1)(5,9); //第一"tuple"必须满参数
int ir = 1;
vec.push_back(boost::make_tuple(1,boost::ref(ir),1));
//降序排列
std::sort(vec.begin(),vec.end(),element_less<0>());
std::cout << "After sorting: " << vec[0].get<0>() << '\n' << vec[1].get<0>() << '\n' << vec[2].get<0>() << '\n';
//升序排列
std::sort(vec.begin(),vec.end(),boost::bind(std::greater<short>(),
boost::bind(&Mtuple::get<0>,_1),
boost::bind(&Mtuple::get<0>,_2)) );
std::cout << "After sorting: " << vec[0].get<0>() << '\n' << vec[1].get<0>() << '\n' << vec[2].get<0>() << '\n';
//绑定tuple
short s;
int d;
double b;
boost::tie(s,d,b)=vec[0];
//for_each
typedef boost::tuple<short,int,long> my_tuple;
boost::tuple<int,short,double> nums(111,222,3.01);
for_each_element(nums, print());
for_each_element(nums, print_type<int>());
//使用 make_tuple 来从函数返回元组
boost::tuple<int, int> t = divide_and_modulo(8, 3);
std::cout << t << '\n';
return 0;
}
#include "boost/lambda/bind.hpp"
using namespace boost::lambda;
那么 bind(&Mtuple::get<0>,_1), 会有问题
有人说是:因为get有const和非const 2个重载版本。得先static_cast指针转换以获取正确的版本。
例子如下:
void foo(int);
void foo(float);
int i;
...
bind(&foo, _1)(i); // error
...
void (*pf1)(int) = &foo;
bind(pf1, _1)(i); // ok
bind(static_cast <void(*)(int)> (&foo), _1)(i); // ok
没看明白。这个问题未完待续。
解决办法是加上命名空间,写出的效果如下:
#include "stdafx.h"
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <map>
#include "boost/tuple/tuple.hpp"
#include "boost/tuple/tuple_comparison.hpp"
#include "boost/tuple/tuple_io.hpp"
#include "boost/array.hpp"
#include "boost/assign/list_of.hpp"
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
using namespace boost;
#include <boost/bind.hpp>
class Person
{
public:
Person(){}
Person(const std::string& name) : name_(name){}
std::string& Name()
{ return name_; }
private:
std::string name_;
};
int main()
{
std::map<int, Person> persons;
persons[123] = Person("Amy");
persons[234] = Person("Ralph");
persons[345] = Person("Simon");
persons[456] = Person("Maggie");
//非lamad::bind 编译有问题?
std::cout << "Ralph's Id is: " <<
std::find_if( persons.begin(), persons.end(),
::lambda::bind(&Person::Name,::lambda::bind(&std::map<int, Person>::value_type::second, ::lambda::_1)) == "Ralph" )->first;
typedef boost::tuple<short,int,long> Mtuple;
std::vector<Mtuple> vec ;
vec= boost::assign::tuple_list_of(3,2,7)(6,2)(7,1)(5,9);
std::cout << "bind tuple: " <<
std::find_if( vec.begin(), vec.end(),
bind(&Mtuple::get<0>,_1) == 7 )->get<1>();
return 0;
}