程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> boost庫的使用_tuple

boost庫的使用_tuple

編輯:C++入門知識

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<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個值的類型。


三 實例

   1)tuple的構造,拷貝構造函數,get成員函數,get全局函數,make_tuple全局函數。 

#include <string>
#include <iostream>
#include "boost/tuple/tuple.hpp"

boost::tuples::tuple<int,double> 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<int,double,std::string>  triple(42,3.14,"My first tuple!");
    boost::tuple<short,int,long> another;
    boost::tuple<int,int,double> another2(10);

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

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

    boost::tuples::tuple<int&>     reft(ref);
    boost::make_tuple(boost::ref(plain));
    boost::make_tuple(boost::ref(ref));
    boost::make_tuple(boost::ref(cref));

    boost::tuples::tuple<const int&> 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<int,double,std::string> 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<int,double> value = get_values();

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

    boost::tuples::tuple<int,int,double> 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<double, char, int> tuplesize;   
    std::tr1::tuple_size();
    std::tr1::tuple_element();*/

}
 

   2)使用tie函數模版來生成對ref的綁定的tuple,tuple的比較使用,tuple的輸入輸出:

#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include "boost/tuple/tuple.hpp"
#include "boost/tuple/tuple_comparison.hpp"
#include "boost/tuple/tuple_io.hpp"
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);
    }
};
int main()
{
    // Tiers are tuples, where all elements are of non-const reference types.
    // They are constructed with a call to the tie function template    
    int i; char c; double d;
    boost::tie(i, c, d) = boost::make_tuple(1,"a", 5.5);
    std::cout << i << " " <<  c << " " << d << std::endl;

    // test ignore
    char ch;
    boost::tie(boost::tuples::ignore, ch) = std::make_pair(1, "a");
    std::cout << ch << std::endl;

    // test for comparison
    boost::tuple<int,std::string> tup1(11,"Match?");
    boost::tuple<short,std::string> tup2(12,"Match?");
    std::cout << std::boolalpha; 
    std::cout << "Comparison: tup1 is less than tup2 "; 
    std::cout << "tup1==tup2: " << (tup1==tup2) << " "; 
    std::cout << "tup1!=tup2: " << (tup1!=tup2) << " ";
    std::cout << "tup1<tup2: " << (tup1<tup2) << " "; 
    std::cout << "tup1>tup2: " << (tup1>tup2) << " "; 
    std::cout << "tup1<=tup2: " << (tup1<=tup2) << " ";
    std::cout << "tup1>=tup2: " << (tup1>=tup2) << " ";
    tup2.get<0>()=boost::get<0>(tup1); //tup2=tup1 also works 
    std::cout << " Comparison: tup1 equals tup2 "; 
    std::cout << "tup1==tup2: " << (tup1==tup2) << " "; 
    std::cout << "tup1!=tup2: " << (tup1!=tup2) << " ";
    std::cout << "tup1<tup2: " << (tup1<tup2) << " ";
    std::cout << "tup1>tup2: " << (tup1>tup2) << " ";
    std::cout << "tup1<=tup2: " << (tup1<=tup2) << " ";
    std::cout << "tup1>=tup2: " << (tup1>=tup2) << " ";

    //test tuple using in the container
    typedef boost::tuple<short,int,long,float,double,long double>  num_tuple;
    std::vector<num_tuple> vec; 
    vec.push_back(num_tuple(6,2));
    vec.push_back(num_tuple(7,1));
    vec.push_back(num_tuple(5)); 
    std::sort(vec.begin(),vec.end(),element_less<1>());
    std::cout << " After sorting: " <<     vec[0].get<0>() << " " <<    vec[1].get<0>() << " " <<    vec[2].get<0>() << " ";


    // test for io
    boost::tuple<float, int, std::string> a(1.0f,  2, std::string("Howdy folks!"));
    std::cout << std::endl << a << std::endl;

    boost::tuple<int, int, int> ii;
   
    std::cin >> ii;
    std::cout << boost::tuples::set_open("[") << boost::tuples::set_close("]")<< boost::tuples::set_delimiter(":");
    std::cout << ii << std::endl;   

    boost::tuples::tuple<int,int,double> tuple1;
    int head = tuple1.get_head();
    double tail = tuple1.get_tail();

}
 

四 注意

1)函數 make_tuple 類似於 std::make_pair. 缺省情況下,make_tuple 設置元素類型為非const, 非引用的,即是最簡單的、根本的參數類

型。

2)為了使一個 tuple 的元素設為引用類型,你要使用函數 boost::ref, 它來自另一個名為 Boost.Ref 的 Boost 庫。

3)如果元素需要是 const 引用的,就使用來自 Boost.Ref 的 boost::cref。

4)如果你要使綁定的每個元素變量都為ref,則可以使用tie函數。

五 參考

1)Beyond the C++ Standard Library: An Introduction to Boost
2)boost在線document

原文鏈接:http://www.cppblog.com/mzty/archive/2007/08/21/30509.html


[python] 
include <boost/tuple/tuple_io.hpp> 
#include <string> 
#include <iostream> 
using namespace std; 
using boost::tuple; 
class A{ 
    int a; 
}; 
 
A class_a; 
boost::tuple<std::string, A> t1("abc", class_a); 
 
int main() 

  
    //boost::tuple<int ,int ,double> tuple_2 = boost::tuple::make_tuple<int, int, do 
    double d = 2.0; 
    A a; 
    tuple<int, double&, const A&> t(1, d, a); 
    const tuple<int, double&, const A&> ct = t; 
 
    int i = boost::get<0>(t); 
    cout<<"the i: "<<i<<endl; 
 
    i = t.get<0>(); 
    cout<<"the i: "<<i<<endl; 
     
    boost::get<1>(t) = 45.34; 
    double d_ret = t.get<1>(); 
    cout<<"the d_ret: "<<d_ret<<endl; 
 
    int tie_i; 
    char tie_char; 
    double tie_doulbe; 
    boost::tuple<int, char, double> tuple_temp(3, 'A', 3.1415); 
    boost::tie(tie_i, tie_char, tie_doulbe) = tuple_temp; 
    tuple_temp.get<2>() = 3.132434; 
    cout<<"the tie_double: "<<tuple_temp.get<2>()<<endl; 
    cout<<"the tie_i: "<<tie_i<<endl; 
    cout<<"the tie_double: "<<tie_doulbe<<endl; 
 
    char char_temp; 
    boost::tie(boost::tuples::ignore, char_temp) = std::make_pair(123, 'a'); 
    cout<< char_temp<<endl; 
   
    tuple<float, int, string> tuple_a(1.3, 2, "abd"); 
    cout<<tuple_a<<endl; 
    cout<<boost::tuples::set_open('[')<<boost::tuples::set_close(']')<<boost::tuples 
    return 0; 

 
 
int main() 

 
    //boost::tuple<int ,int ,double> tuple_2 = boost::tuple::make_tuple<int, int, double>(5, 10, 23.2); 
    double d = 2.0; 
    A a; 
    tuple<int, double&, const A&> t(1, d, a); 
    const tuple<int, double&, const A&> ct = t; 
 
    int i = boost::get<0>(t); 
    cout<<"the i: "<<i<<endl; 
 
    i = t.get<0>(); 
    cout<<"the i: "<<i<<endl; 
 
    boost::get<1>(t) = 45.34; 
    double d_ret = t.get<1>(); 
    cout<<"the d_ret: "<<d_ret<<endl; 
 
    int tie_i; 
    char tie_char; 
    double tie_doulbe; 
    boost::tuple<int, char, double> tuple_temp(3, 'A', 3.1415); 
    boost::tie(tie_i, tie_char, tie_doulbe) = tuple_temp; 
    tuple_temp.get<2>() = 3.132434; 
    cout<<"the tie_double: "<<tuple_temp.get<2>()<<endl; 
    cout<<"the tie_i: "<<tie_i<<endl; 
    cout<<"the tie_double: "<<tie_doulbe<<endl; 
 
    char char_temp; 
    boost::tie(boost::tuples::ignore, char_temp) = std::make_pair(123, 'a'); 
    cout<< char_temp<<endl; 
 
    tuple<float, int, string> tuple_a(1.3, 2, "abd"); 
    cout<<tuple_a<<endl; 
    cout<<boost::tuples::set_open('[')<<boost::tuples::set_close(']')<<boost::tuples::set_delimiter('\t')<<tuple_a; 
    return 0; 


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved