Chinaunix首页 | 论坛 | 博客
  • 博客访问: 164574
  • 博文数量: 36
  • 博客积分: 1466
  • 博客等级: 上尉
  • 技术积分: 380
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-17 17:43
文章分类

全部博文(36)

分类: C/C++

2007-07-04 11:48:37

Learning boost 4 Tuple and ref
 
原地址: 非常抱歉, 忘记在哪找来的, 请原作者见谅
 
Tuple
Boost::tuple是类似于std::pair的一个类。Pair有且只能有两个成员(first和second),而tuple的元素可以是0-10个。
使用tuple要包含boost/tuple/tuple.hpp文件。
例如:
#include
...
tuple t1(1,2.0,”Hello”);
tuple t2(1,’a’);
tuple t3;//在这种情况下,t3的两个int值是为定义
tuple t4(1);//只初始化第一个元素
tuple的元素使用get函数或get成员函数访问。例如:
tuple t(1,2.0,”Hello”);
cout<()<
t.get<1>()=2.0;//将t的第二个元素赋值为2.0;
//以上都是使用get成员函数,下面使用全局的get函数
cout<(t)<
get<1>(t)=2.0;
tuple的元素还可以使用引用类型。例如:
int a;
tuple<&int,int> t(a,1);
t.get<0>()=123;
cout<
 
tuple<&int> t1;//这是错的,引用类型一定要初始化
tuple<&int> t2(1);//这也是错的,不能使用常数初始化引用类型
tuple还可以使用流直接输出,但是一定要包含tuple_io.hpp文件。例如:
#include//tuple_io.hpp中已经包含tuple.hpp文件
tuple t(2,”hello”,1.0);
cout<
//tuple默认使用’(‘开始,’)’结尾,空格分隔的方式输出。可以使用操纵器改变。
cout<
     <
     <
     <
tuple同样有==,!=,>,<等比较运算符的重载,大于小于的使用字典顺序比较。
同pair一样,tuple有make_tuple辅助函数来构造tuple。例如:
cout<
如果要构造引用类型,要使用ref辅助函数。例如:
int a=123;
cout<
cout<
 
int a,b,c;
tuple t(a,b,c);
t=make_tuple(1,2,3);//这时a==1 b==2 c==3
对于这样的应用,可以使用更为方便的tie函数。例如:
int a,b,c;
tie(a,b,c)=make_tuple(1,2,3);
如果要其中的某几个元素,可以使用tuples::ignore代替不需要使用的部分。例如:
tie(a,tuples::ignore,c)=make_tuple(1,2,3);
tuple的性能:
     cout<)<
     cout<)<
     cout<)<
     cout<)<
     cout<)<
     cout<)<
tuple的访问,也是和pair具有相同的效率。
 
Ref
在tuple中,我们使用到了ref和cref辅助函数,那么ref到底是什么呢?
ref(a),其实是返回了一个reference_wrapper类,这个类保存着指向a的指针。
template class reference_wrapper
{
public:
    typedef T type;
 
#if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
    explicit reference_wrapper(T& t): t_(&t) {}
#else
    explicit reference_wrapper(T& t): t_(boost::addressof(t)) {}
#endif
 
    operator T& () const { return *t_; }
    T& get() const { return *t_; }
    T* get_pointer() const { return t_; }
 
private:
    T* t_;
};
以上就是reference_wraper的全部代码。其中operator T&()const返回的是*t_的应用,那么对于reference_wrapper的任何操作都会施加于*t_,通过reference_wrapper可以将某些值传递的参数转换成引用传递。例如:
#include
#include
using namespace std;
using namespace boost;
template void inc(T t)
{
     ++t;//由于reference_wrapper并没有重载++运算符,所有被隐式转换为int&,也就是对a的引用。
}
int main()
{
    int a=1;
     inc(ref(a));//构造reference_wrapper
     cout<
     return 0;
}
有了reference_wrapper,我们可以轻松构造引用语义的stl容器,注意vector是错误的哦。
     vector > ref_vector;
     int a[10];
     for (int i=0;i<10;++i)
         ref_vector.push_back(ref(a[i]));
 
 
阅读(1280) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~