boost库的使用_tuple(一)

2014-11-24 10:45:13 · 作者: · 浏览: 0

tuple的使用
一 Boost::tuple
很多的时候我们经常需要为我们的函数返回多个值,一般的做法是通过传入非常量的指针或引用,但是这样的话可能可读性就要差一些,使用者可能需要确切的文档才能确定到底哪个是返回值,为了更好的可读性,我们可以使用class或struct来封装我们要返回的多个值,然后返回封装struct或class,但是使用这种方法的弊端就是增加的程序的代码量,最好的解决办法其实我们可以通过一种匿名的struct或class来解决这个问题。

Boost::tuple就为我们提供了一种类似于匿名struct的方法为我们解决函数的多个返回值的问题。既增强了代码的可读性有不增加代码量。其实在STL中已经有这样的特例,std::pair其实就是boost::tuple的2个参数的特例,对boost::tuple你可以绑定更多的参数,或者你可以迭代实现无限多参数的情况。

二 源码剖析

头文件: "boost/tuple/tuple.hpp",它包含了 tuple 类模板及库的核心部分。

头文件: "boost/tuple/tuple_io.hpp",包含了对 tuple 的输入输出操作符。

头文件: "boost/tuple/tuple_comparison.hpp",包含了 tuple 的关系操作符。

为了方便使用,Tuple 库中有些名字位于名字空间 boost:如 tuple, make_tuple, tie, 和 get.

函数说明:

1)构造函数
2)拷贝构造函数
3)t.get()或get(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个值的类型。


三 实例

1)tuple的构造,拷贝构造函数,get成员函数,get全局函数,make_tuple全局函数。

#include
#include
#include "boost/tuple/tuple.hpp"

boost::tuples::tuple get_values()
{
return boost::make_tuple(6,12.0);
}
class base
{
public:
virtual ~base() {};
virtual void test()
{
std::cout << "base::test() ";
}
};
class derived : public base
{
public:
virtual void test() { std::cout << "derived::test() "; }
};

void main()
{
// test for constructor
boost::tuple triple(42,3.14,"My first tuple!");
boost::tuple another;
boost::tuple another2(10);

// test for make_tuple , ref and cref function
int plain=42;
int& ref=plain;
const int& cref=ref;

boost::tuples::tuple plaint(plain);
plaint = boost::make_tuple(plain);
plaint = boost::make_tuple(ref);
plaint = boost::make_tuple(cref);

boost::tuples::tuple reft(ref);
boost::make_tuple(boost::ref(plain));
boost::make_tuple(boost::ref(ref));
boost::make_tuple(boost::ref(cref));

boost::tuples::tuple creft(cref);
boost::make_tuple(boost::cref(plain));
boost::make_tuple(boost::cref(ref));
boost::make_tuple(boost::cref(cref));


// test for get function
boost::tuple triple2(42,3.14,"The amazing tuple!");
int i=boost::tuples::get<0>(triple2);
double d=triple2.get<1>();
std::string s=boost::get<2>(triple2);

// test for function return tuple
boost::tuples::tuple value = get_values();

// test for copy constructor
boost::tuple tup1(-5,"Tuples");
boost::tuple tup2;
tup2=tup1;
tup2.get<2>().test();
std::cout << "Interesting value: " << tup2.get<0>() << " ";
const boost::tuple tup3(tup2);
//tup3.get<0>()=3.14; // error, because tup3 is const

boost::tuples::tuple tuple1(10,30,20.000);
int head = tuple1.get_head();
int tailhead = tuple1.get_tail().get_head();
double tail = tuple1.get_tail().get_tail().get_head();

// for TR1
/*boost::tuples::tuple tuplesize;
std::tr1::tuple_size();
std::tr1::tuple_element();*/

}

2)使用tie函数模版来生成对ref的绑定的tuple,tuple的比较使用,tuple的输入输出:

#include
#include
#include
#include
#include "boost/tuple/tuple.hpp"
#include "boost/tuple/tuple_comparison.hpp"
#include "boost/tuple/tuple_io.hpp"
template
class element_less
{
public:
template
bool operator()(const Tuple& lhs,const Tuple& rhs) const
{
return boost::get(lhs)(rhs);