关闭

boost之tuple

标签: boosttuple
134人阅读 评论(0) 收藏 举报
分类:

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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:30939次
    • 积分:1151
    • 等级:
    • 排名:千里之外
    • 原创:85篇
    • 转载:2篇
    • 译文:0篇
    • 评论:10条
    最新评论