boost之tuple

tuple的基本用法

#include <iostream>
#include <boost/typeof/typeof.hpp>
#include <boost/assign.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_io.hpp>
using namespace std;
using namespace boost;
using namespace boost::assign;
using namespace boost::tuples;

int main()
{
    ///////初始化
    typedef boost::tuple<int, string, double> my_tuple;
    my_tuple t1;
    my_tuple t2(1,"lijiajia");
    my_tuple t3(t1);
    my_tuple t4 = t1;

    ////元素类型是引用的时候,初始化时必须赋初值
    int x = 10;
    boost::tuple<int&>t5(x);

    ///////make_tuple
    make_tuple(2,3.0);
    make_tuple(string(),vector<int>());

    /////访问元素使用t.get<>()
    BOOST_AUTO(tt,make_tuple(1,"char[]",100.0));
    assert(tt.get<0>() == 1);
    assert(tt.get<1>() == "char[]");
    assert(tt.get<2>() == 100.0);

    //////访问元素使用boost::get<>()
    cout<<get<0>(tt);
    assert(get<0>(tt) == 1);
    assert(get<1>(tt) == "char[]");
    assert(get<2>(tt) == 100.0);

    make_tuple(1, 11.11, string("lijiajia"));

    ////////比较操作
    typedef boost::tuples::tuple <int, double, string>my_tuple1;
    //my_tuple1 tt1 = make_tuple(1, 11.11, string("lijiajia"));/////不起效果  why boost::tuples::make_tuple(1, 11.11, string("lijiajia"))
    //my_tuple1 tt2 = make_tuple(2,22.22,string("caoyanyan"));/////不起效果   why boost::tuples::make_tuple(1, 11.11, string("lijiajia"))
    my_tuple1 tt1(1, 11.11, string("lijiajia"));
    my_tuple1 tt2(2, 22.22, string("caoyanyan"));
    my_tuple1 tt3(tt2);
    assert(tt1 > tt2);
    assert(tt2 == tt3);


    /////输入输出

    cout << "tt1 = " << tt1 << endl;/////不起效果  why  加上#include <boost/tuple/tuple_io.hpp>
    cout << "tt2 = " << tt2 << endl;/////不起效果  why  加上#include <boost/tuple/tuple_io.hpp>
    cout << "please input the tt1:" << endl;
    cin >> tt1;/////不起效果  why   加上#include <boost/tuple/tuple_io.hpp>
    cout <<"tt1 = " <<tt1 << endl;

    cout << "after set_open set_close set_delimiter....." << endl;
    cout << tuples::set_open('[') << tuples::set_close(']');
    cout << tuples::set_delimiter(',');
    cout << tt1 << endl;



    /////tie的使用
    int i;
    double d;
    string s;
    tie(i, d, s) = boost::tuples::make_tuple(1,22.2,"lijiajia");
    cout << "i = " << i << endl << "d = " << d << endl << "s = " << s << endl;

    /////占位符的使用tuples::ignore
    tie(i, tuples::ignore, tuples::ignore) = boost::tuples::make_tuple(1,NULL,NULL);


    ////tie使用于pair
    tie(i, s) = make_pair(1,"lijiajia_caoyanyan");
    cout << "i = " << i << endl<< "s = " << s << endl;


    //////与assign结合使用
    vector<my_tuple1> my_vec = tuple_list_of(1,11.11,"lijiajia")(2,22.22,"caoyanyan");
    assert(my_vec.size() == 2);
    my_vec += boost::tuples::make_tuple(3, 33.33, "dabao"), boost::tuples::make_tuple(4, 44.44, "baierwei");
    assert(my_vec.size() == 4);


    system("pause");
    return 0;
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_31839479/article/details/53996187
文章标签: boost tuple
个人分类: boost
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

boost之tuple

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭