创建tuple

    1. make_tupe
    tuple<const char*, int> tp = make_tuple(sendPack, nSendSize);//构造tuple
    2. tie 函数(创建一个元组的左值引用)
    auto tp = std::tie(1, "aa", 2.2); //tp 实际类型为 std::tuple<int&, string&, int&>

 

解析tuple

    1. get<x>()
    const char* data = tp.get<0>();
    int a = tp.get<1>();
    2. tie
    int x, y;
    string a;
    std::tie(x, a, y) = tp; //则自动赋值到三个变量
    也可以只解析第三个值:
    std::tie(std::ignore, std::ignore, y) = tp; //std::ignore为占位符

 

连接tuple

int main(){
    std::tuple<int, std::string, float> t1(10, "test", 3.14);
    int n = 7;
    auto t2 = std::tuple_cat(t1, std::make_pair("Foo", "nar"), t1, std::tie(n));
    n = 10;
    //t2 = {10, "test", 3.14, "Foo", "nar", 10}; //n为10,因为tie中存放的为引用
    return 0;
}

 

+

相关文章: