boost:tuple

tuple
功能:
  从函数中返回多个值
  将相关的类型分组
Header: "boost/tuple/tuple.hpp" tuple类模板和库的核心
Header: "boost/tuple/tuple_io.hpp" tuple的输入输出操作
Header: "boost/tuple/tuple_comparison.hpp" tuple的比较操作
成员函数
1.构造函数
    tuple();
 
    template <class P1,class P2...,class PM>
      tuple(class P1,class P2,...,PN);
   
    template <class U1,class U2,...,class UN>
    tuple(const tuple<U1,U2,...,UN>&);

2.  TIndex & get<int Index>();
    const TIndex & get<int Index>() const;
    返回第Index个成份的引用。Index必须是整数,如果index大于tuple的元素总数产生编译错误。
3.  tuple& operator=(const tuple& other);



template<class T1,class T2,...,class TN> tuple<V1,V2,...,VN>
  make_tuple(const T1& t1,const T2& t2,...,const TN& tn);
与std::make_pair类似

template<class T1,class T2,...,class TN> tuple<T1&,T2&,...,TN>
  tie(T1& t1,T2& t2,...,TN& tn);
tie(t1,t2,...,tn)等同于make_tuple(ref(t1),ref(t2)... ref(tn))

template <int I,class T1,class T2,...,class TN>
  RI get(tuple<T1,T2,...,TN>& t);
获得tuple t的一个元素。如果TI是一个引用类型,RI=TI,反之,RI=TI&


相关操作
bool operator==(
  const tuple<T1,T2,...,TN>& lhs,
  const tuple<U1,U2,...,UN>& rhs);
bool operator!=(
  const tuple<T1,T2,...,TN>& lhs,
  const tuple<U1,U2...,...,>& rhs);
bool operator<(
  const tuple<T1,T2,...,TN>& lhs,
  const tuple<U1,U2,...,UN>& rhs);

使用
如果元素是引用类型,用boost::ref。例如 boost::make_tuple(boost::ref(plain));
1.获得tuple的元素
#include <iostream>
#include <string>

#include "boost/tuple/tuple.hpp"

int main() {
  boost::tuple<int,double,std::string>
  triple(42,3.14,"The amazing tuple!");

  int i=boost::tuples::get<0>(triple);
  double d=triple.get<1>();
  std::string s=boost::get<2>(triple);
}

2.tie的使用
  不使用tie
  boost::tuple<int,int> tup;
  tup=gcd_lcm(12,18);
  int gcd=tup.get<0>());
  int lcm=tup.get<1>());

  使用
  boost::tie(gcd,lcm)=gcd_lcm(15,20);

3.输出tuple

posted on 2008-07-01 22:57 guaiguai 阅读(665) 评论(0)  编辑 收藏 引用 所属分类: boost


只有注册用户登录后才能发表评论。
相关文章:
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

常用链接

留言簿(1)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜