zoukankan      html  css  js  c++  java
  • STL其他--<tuple>用法【C11】

     tuple 库

      tuple 库 是能够将不同类型的对象组合起来形成一个对象。和pair 对象一样,但是可以组织多种不同类型的元素。

      tuple中的元素类型是在编译时期决定的。与<utility>中的pair 类密切相关, pair对象可以视为tuple类型对待。

      头文件: <tuple>,

    1. tuple 类

      原型:  template <class... Types> class tuple;

      tuple对象可以组合不同元素, 每个元素的类型可以不同;

    2. 成员函数

    (constructor)

    (1)默认构造函数,

      构造tuple对象用元素初始值;

    (2)copy/move 构造函数

      通过另一个tuple对象来构造tuple对象

    (3)隐式转换构造函数;

    (4)初始自定义构造函数;

    (5)pair 转化 构造函数

    示例:

    // tuple constructors
    #include <iostream>     // std::cout
    #include <utility>      // std::make_pair
    #include <tuple>        // std::tuple, std::make_tuple, std::get
    
    int main ()
    {
      std::tuple<int,char> first;                             // default
      std::tuple<int,char> second (first);                    // copy
      std::tuple<int,char> third (std::make_tuple(20,'b'));   // move
      std::tuple<long,char> fourth (third);                   // implicit conversion
      std::tuple<int,char> fifth (10,'a');                    // initialization
      std::tuple<int,char> sixth (std::make_pair(30,'c'));    // from pair / move
    
      std::cout << "sixth contains: " << std::get<0>(sixth);
      std::cout << " and " << std::get<1>(sixth) << '
    ';
    
      return 0;
    }

     operator= 赋值函数

    tuple:: swap 函数

     void swap (tuple& tpl)

      交换参数中的每个元素值。

    3. Non-member function overloads

     (1)逻辑比较

      顺序比较两个不同的元素,指导发面不同,即作为结果。返回bool值。

    (2) std::swap(a,b);

    (3) std::get<i>(mytuple)

      返回tuple 对象中的第i个元素的引用。

      get同样用于tuple类似的类型,如pair, array等。

      注意, 模板参数i来决定获取的索引位置,且必须是constexpr,即编译器为const value

      返回参会参数是: tuple_element<i, tuple<type1, type2,...>>:: type 是 type对象中第i个元素的类型。

    4. Helper classes

    (1)std::tuple_size<T>类

      用来获取tuple 对象中的元素数量的类模板。

      模板参数T ,用于访问的tuple对象的类型。 即一个已经存在的类的声明。

      类成员变量:value, 即类型T中的元素数量。

    / tuple_size
    #include <iostream>     // std::cout
    #include <tuple>        // std::tuple, std::tuple_size
    
    int main ()
    {
      std::tuple<int,char,double> mytuple (10,'a',3.14);
    
      std::cout << "mytuple has ";
      std::cout << std::tuple_size<decltype(mytuple)>::value;
      std::cout << " elements." << '
    ';
    
      return 0;
    }

    (2)std::tuple_element<>类

      被设计用来访问tuple对象中第i个元素类型的类。

      具有成员类型 type, 即通过std::get<i>(tuple)访问第i个元素的类型。

    #include <iostream>     // std::cout
    #include <tuple>        // std::tuple, std::make_tuple, std::tuple_element, std::get
    
    int main ()
    {
      auto mytuple = std::make_tuple (10,'a');
    
      std::tuple_element<0,decltype(mytuple)>::type first = std::get<0>(mytuple);
      std::tuple_element<1,decltype(mytuple)>::type second = std::get<1>(mytuple);
    
      std::cout << "mytuple contains: " << first << " and " << second << '
    ';
    
      return 0;
    }

    5.Functions

    生成对象:

      std::make_tuple<class... Types>

      构建一个合适tuple类型的对象,并包含执行元素类型的参数。

      函数返回的类型tuple<type,,,> 是在函数模板中定义的参数。

      函数通过call tuple的初始构造函数。

    // make_tuple example
    #include <iostream>
    #include <tuple>
    #include <functional>
    
    int main()
    {
      auto first = std::make_tuple (10,'a');             // tuple < int, char >
    
      const int a = 0; int b[3];                         // decayed types:
      auto second = std::make_tuple (a,b);               // tuple < int, int* >
    
      auto third = std::make_tuple (std::ref(a),"abc");  // tuple < const int&, const char* >
    
      std::cout << "third contains: " << std::get<0>(third);
      std::cout << " and " << std::get<1>(third);
      std::cout << std::endl;
    
      return 0;
    }

    endl;

  • 相关阅读:
    python的xpinyin模块:汉字转拼音
    中文报错SyntaxError: Non-UTF-8 code starting with 'xe6' in file
    "底层逻辑”是什么意思?
    【Golang】关于Go中logrus的用法
    【Golang】 关于Go语言中的锁
    【Golang】Go 通过结构(struct) 实现接口(interface)
    【Golang】Go语言之log的使用
    【Golang】Go中时间(time)的用法以及gorm处理时间戳
    【Golang】Go中三个点(...)用法
    一文彻底掌握Apache Hudi异步Clustering部署
  • 原文地址:https://www.cnblogs.com/icmzn/p/8656989.html
Copyright © 2011-2022 走看看