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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值