构造

  构造函数

   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

C++标准库之tuple的更多相关文章

  1. C/C++基础----标准库几个工具库tuple,bitset,正则表达式,随机数,IO库

    tuple tuple可以有任意多个成员 默认初始化,值初始化 构造函数是explicit,必须直接初始化 make_tuple(v1,v2,-,vn) get<i> (t) 返回第i个数 ...

  2. Python标准库13 循环器 (itertools)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在循环对象和函数对象中,我们了解了循环器(iterator)的功能.循环器是对象的 ...

  3. 【循序渐进学Python】11.常用标准库

    安装完Python之后,我们也同时获得了强大的Python标准库,通过使用这些标准库可以为我们节省大量的时间.这里是一些常用标准库的简单说明.更多的标准库的说明,可以参考Python文档 sys 模块 ...

  4. Boost程序库完全开发指南——深入C++“准”标准库(第3版)

    内容简介  · · · · · · Boost 是一个功能强大.构造精巧.跨平台.开源并且完全免费的C++程序库,有着“C++‘准’标准库”的美誉. Boost 由C++标准委员会部分成员所设立的Bo ...

  5. 读书笔记 effective c++ Item 54 让你自己熟悉包括TR1在内的标准库

    1. C++0x的历史渊源 C++标准——也就是定义语言的文档和程序库——在1998被批准.在2003年,一个小的“修复bug”版本被发布.然而标准委员会仍然在继续他们的工作,一个“2.0版本”的C+ ...

  6. python 标准库 -- subprocess

    subprocess 主要功能室执行外部的命令和程序 一个进程可 fork 一个子进程, 并让这个子进程 exec 另外一个程序. 在 python 中, 可以通过标准库中的 subprocess 包 ...

  7. C++相关:部分标准库特殊设施

    C++ tuple(元组) tuple是C++11新标准里的类型.它是一个类似pair类型的模板.pair类型是每个成员变量各自可以是任意类型,但是只能有俩个成员,而tuple与pair不同的是它可以 ...

  8. collections标准库

    collections标准库 之前Python的第三方库用的一直很舒服,现在突然发现标准库也有collections这样的神器,可以补充list.set.dict以外的应用 1. namedtuple ...

  9. python第六天 函数 python标准库实例大全

    今天学习第一模块的最后一课课程--函数: python的第一个函数: 1 def func1(): 2 print('第一个函数') 3 return 0 4 func1() 1 同时返回多种类型时, ...

随机推荐

  1. Debian安装VirtualBox增强工具

    切换到root用户: apt-get install build-essential 或者 apt-get install gcc make apt-get install dkms apt-get ...

  2. 简单php连接数据库作操作

    1.近期稳定版本 <?php header('Content-Type: application/json'); $output = []; $host = ''; //MySQL服务器地址 $ ...

  3. ajax请求不能下载文件(转载)

    最近在做文件下载,后台写了个控制层,直接走进去应该就可以下载文件,各种文件图片,excel等 但是起初老是下载失败,并且弹出下面的乱码: 前台请求代码: $('#fileexcel').unbind( ...

  4. tengine + lua 实现流量拷贝

    环境搭建参考地址:http://www.cnblogs.com/cp-miao/p/7505910.html cp.lua local res1, res2, action action = ngx. ...

  5. [转载]Delphi Tokyo 10.2.3发布了

    转载:http://blog.sina.com.cn/s/blog_44fa172f0102wwwg.html 今早最好的消息,Delphi 10.2.3如期发布,下载地址:http://altd.e ...

  6. servlet虚拟路径映射

    在web.xml文件中,一个<servlet-mapping>元素用于映射一个Servlet的对外访问路径,该路径也称为虚拟路径.例如<url-pattern>/TestSer ...

  7. HBase设计与开发

    HBase设计与开发 @(HBase) 适合HBase应用的场景 成熟的数据分析主题,查询模式已经确定且不会轻易改变. 传统数据库无法承受负载. 简单的查询模式. 基本概念 行健:是hbase表自带的 ...

  8. BEGINNING SHAREPOINT&#174; 2013 DEVELOPMENT 第14章节--使用Office Services开发应用程序 新的机器翻译服务

    BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第14章节--使用Office Services开发应用程序  新的机器翻译服务         机器翻译服务也是继Wor ...

  9. HTML5游戏探讨,怎样让微信游戏仅仅能执行在微信中

    大致文件布局例如以下.一个html文件.一个loading.js,在loading.js中载入其它须要的js和css. 至于详细的速度的话.建议cdn或者一个域中最多载入6个js文件.在loading ...

  10. Android - 错误:Unable to instantiate application

    错误:Unable to instantiate application 本文地址: http://blog.csdn.net/caroline_wendy 错误:java.lang.RuntimeE ...