• C++11-元组


    范型元组

    tuple 可以理解为是对老版本pair类模板的扩展,其中的元素个数不再限于两个,而且功能更加丰富

    tuple:构造tuple对象所保存的只是构造实参的拷贝

    make_tuple:构造tuple对象所保存的只是构造实参的拷贝

    tie:通过tie构造的tuple对象,保存构造实参的可写引用

    forward_as_tuple:通过forward_as_tuple构造的tuple对象,保存构造实参的原始引用,左值使用左值引用,右值使用右值引用

    参见:tuple.cpp

    #include <iostream>
    #include <tuple>
    using namespace std;
    int main (void) 
    {
    string name = "张飞"; int age = 25; float height = 1.75; // 直接构造tuple对象 tuple<string, int, float> st1 (name, age, height); // 获取tuple元素的值 cout << get<0> (st1) << endl; cout << get<1> (st1) << endl; cout << get<2> (st1) << endl; // 通过make_tuple构造tuple对象 auto st2 = make_tuple (name, age, height); cout << get<0> (st2) << endl; cout << get<1> (st2) << endl; cout << get<2> (st2) << endl; get<0> (st2) = "赵云"; get<1> (st2) = 20; get<2> (st2) = 1.65; cout << get<0> (st2) << endl; cout << get<1> (st2) << endl; cout << get<2> (st2) << endl; cout << name << endl; cout << age << endl; cout << height << endl; // 按以上两种方式构造的tuple对象所保存的只是构造实参的拷贝 // 通过tie构造tuple对象,保存构造实参的可写引用 auto st3 = tie (name, age, height); cout << get<0> (st3) << endl; cout << get<1> (st3) << endl; cout << get<2> (st3) << endl; get<0> (st3) = "赵云"; get<1> (st3) = 20; get<2> (st3) = 1.65; cout << get<0> (st3) << endl; cout << get<1> (st3) << endl; cout << get<2> (st3) << endl; cout << name << endl; cout << age << endl; cout << height << endl; // 通过tie解析tuple对象 auto st4 = make_tuple ("关羽", 30, 1.85); tie (name, age, height) = st4; cout << name << endl; cout << age << endl; cout << height << endl; // 通过forward_as_tuple构造tuple对象,保存构造实参的原始引用 auto st5 = forward_as_tuple (name, age, height); cout << &get<0> (st5) << ' ' << &name << endl; cout << &get<1> (st5) << ' ' << &age << endl; cout << &get<2> (st5) << ' ' << &height << endl; get<0> (st5) = "赵云"; get<1> (st5) = 20; get<2> (st5) = 1.65; cout << name << endl; cout << age << endl; cout << height << endl; auto st6 = forward_as_tuple ("关羽", 30, 1.85); cout << &get<0> (st6) << ' ' << &name << endl; cout << &get<1> (st6) << ' ' << &age << endl; cout << &get<2> (st6) << ' ' << &height << endl; /* 无法对右值引用赋值 get<0> (st6) = "赵云"; get<1> (st6) = 20; get<2> (st6) = 1.65; */ return 0; }
  • 相关阅读:
    .NET 2.0 WinForm Control DataGridView 编程36计(一)
    Sql Server 日期格式化函数
    FastReport 金额大小写转换自定义函数
    vue.js 三种方式安装(vuecli)
    Android style
    android ui 布局性能优化
    android 手机分辨率
    TCP,IP,HTTP,SOCKET区别和联系
    android2.2 特性
    常见开放api平台OpenAPI
  • 原文地址:https://www.cnblogs.com/LuckCoder/p/8632209.html
Copyright © 2020-2023  润新知