zoukankan      html  css  js  c++  java
  • C++标准库之tuple

    构造

      构造函数

       tuple的构造函数很普通,没啥说的。

    default (1)
    constexpr tuple();默认构造函数
    
    copy / move (2)
    tuple (const tuple& tpl) = default;  拷贝构造函数
    tuple (tuple&& tpl) = default;移动构造函数
    
    implicit conversion (3)
    template <class... UTypes>
      tuple (const tuple<UTypes...>& tpl);  广义拷贝构造函数,只要对应参数可以默认转换
    template <class... UTypes>
      tuple (tuple<UTypes...>&& tpl); 广义移动构造函数,只要对应参数可以默认转换
    
    initialization (4)
    explicit tuple (const Types&... elems);构造并初始化
    template <class... UTypes>
      explicit tuple (UTypes&&... elems);构造并初始化,参数类型只要求可以默认转换
    
    conversion from pair (5)
    template <class U1, class U2>
      tuple (const pair<U1,U2>& pr);   从pair构造
    template <class U1, class U2>
      tuple (pair<U1,U2>&& pr);
    

    allocator (6)

    在以上5类的基础上增加分配器参数,第一个参数无实际意义,只用来与(3)进行区别。

    template<class Alloc>
      tuple (allocator_arg_t aa, const Alloc& alloc);
    template<class Alloc>
      tuple (allocator_arg_t aa, const Alloc& alloc, const tuple& tpl);
    template<class Alloc>
      tuple (allocator_arg_t aa, const Alloc& alloc, tuple&& tpl);
    template<class Alloc,class... UTypes>
      tuple (allocator_arg_t aa, const Alloc& alloc, const tuple<UTypes...>& tpl);
    template<class Alloc, class... UTypes>
      tuple (allocator_arg_t aa, const Alloc& alloc, tuple<UTypes...>&& tpl);
    template<class Alloc>
      tuple (allocator_arg_t aa, const Alloc& alloc, const Types&... elems);
    template<class Alloc, class... UTypes>
      tuple (allocator_arg_t aa, const Alloc& alloc, UTypes&&... elems);
    template<class Alloc, class U1, class U2>
      tuple (allocator_arg_t aa, const Alloc& alloc, const pair<U1,U2>& pr);
    template<class Alloc, class U1, class U2>
      tuple (allocator_arg_t aa, const Alloc& alloc, pair<U1,U2>&& pr);
    获取tuple的分量
      get模板函数可以获取tuple分量的引用,如下图所声明的,常量tuple获得常量引用,右值引用tuple获得右值引用,非常量非右值引用获得引用。
    (1)
    template <size_t I, class... Types>
    typename tuple_element< I, tuple<Types...> >::type& get(tuple<Types...>& tpl) noexcept;
    
    (2)
    template <size_t I, class... Types>
    typename tuple_element< I, tuple<Types...> >::type&& get(tuple<Types...>&& tpl) noexcept;
    
    (3)
    template <size_t I, class... Types>
    typename tuple_element< I, tuple<Types...> >::type const& get(const tuple<Types...>& tpl) noexcept;

    std::make_tuple

      template<class... Types>
        tuple<VTypes...> make_tuple (Types&&... args);  
      make_tuple模板函数,根据实参类型生成一个tuple,并用实参的值对其进行初始化。编译器在推断tuple各分量的类型时会去掉实参的顶级const属性、引用属性(包括右值引用),也就是说造出来的tuple存的是值。数组会推断为指针、函数会推断为函数指针。如果需要推断出引用类型,要借助std::ref或std::cref。
     
      

     

    std::tie

    template<class... Types>
      constexpr tuple<Types&...> tie (Types&... args) noexcept;
      tie生成一个tuple,此tuple包含的分量全部为实参的引用,与make_tuple完全相反。主要用于从tuple中提取数据。例如:
      int a,b,c;
      auto x = make_tuple(1,2,3);
      std::tie(a,b,c) = x;


    std::forward_as_tuple

    template<class... Types>

    constexpr tuple<Types&&...> forward_as_tuple(Types&&...)noexcept;

    同std::tie一样,也是生成一个全是引用的tuple,不过std::tie只接受左值,而std::forward_as_tuple左值、右值都接受。主要是用于不损失类型属性的转发数据。

    std::tuple_cat

    template<class... Tuples>

    tuple<CTypes...> tuple_cat(Tuples&&... tlps);

    此函数接受多个tuple作为参数,然后返回一个tuple。返回的这个tuple将tuple_cat的参数中的tuple的所有元素按所属的tuple在参数中的顺序以及其在tuple中的顺序排列成一个新的tuple。新tuple中元素的类型与参数中的tuple中的元素的类型完全一致。

    template<class... Types>

    struct tuple_size<tuple<Types...>>;

    tuple_size为辅助类,用于获取tuple中元素的个数。用法为:tuple_size<decltype(tuple)>::value

    template<size_t I,class... Types>

    struct tuple_element<I,tuple<Types ...>>;

    tuple_element为辅助类,用于获取tuple中某个元素的类型。用法为:tuple_size<1,decltype(tuple)>::type

  • 相关阅读:
    sql server常用
    Building Workspace has encountered a problem
    交换机端口安全Port-Security超级详解
    nginx网站502与504错误分析
    基于IP的nginx反向代理示例
    OpenMediaVault(OMV)安装omv-extras命令
    百度编辑器不能插入html标签解决方法
    如何解决关于ueditor编辑器过滤script/style标签的问题
    inux反选删除文件
    Linux下每天自动备份Mysql数据库发送到指定Email
  • 原文地址:https://www.cnblogs.com/vsuu/p/4090355.html
Copyright © 2011-2022 走看看