构造

  构造函数

   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. 64个命令,每天一个linux命令目录, shutdown,tee,rcp,

    每天一个linux命令目录 开始详细系统的学习linux常用命令,坚持每天一个命令,所以这个系列为每天一个linux命令.学习的主要参考资料为: 1.<鸟哥的linux私房菜> 2.htt ...

  2. windows 手动安装 PostgreSQL 9.0 zip解压缩版

    一.下载解压 1,下载postgresql-9.0.1-1-windows-binaries.zip 2,解压postgresql-9.0.1-1-windows-binaries.zip到D:\ 二 ...

  3. HA分布式集群一hadoop+zookeeper

    一:HA分布式配置的优势: 1,防止由于一台namenode挂掉,集群失败的情形 2,适合工业生产的需求 二:HA安装步骤: 1,安装虚拟机 1,型号:VMware_workstation_full_ ...

  4. docker 安装 gogs(go git server) 及问题解决

    docker安装gogs 参考官方说明 gogs mysql 支持 运行一个mysql image docker run --name gogs-mysql -e MYSQL_ROOT_PASSWOR ...

  5. Windows如何自定义U盘盘符、文件夹图标、文件夹背景

    自定义U盘盘符.文件夹图标.文件夹背景 注意对于Vista和Win7的用户不支持文件夹图标和文件夹背景的更换 1.自定义盘符:在U盘根目录下新建文件 autorun.inf(可先建.txt文本文档,再 ...

  6. 【转】Python——编码规范

    来自于 啄木鸟社区 Python Coding Rule --- hoxide 初译 dreamingk 校对发布 040724 --- xyb 重新排版 040915 --- ZoomQuiet M ...

  7. openerp-server.conf 中配置 dbfilter 参数无效的解决办法

    来自:http://shine-it.net/index.php/topic,14517.html 以前就发现过这个问题, 今天重新在群里同大家讨论了一下. 有时候可能我们希望用户不从登陆界面的账套选 ...

  8. 用尽洪荒之力整理的Mysql数据库32条军规(转)

    今天上午吐血整理了Oracle SQL性能优化的40条军规,其中很多规则也是适用于Mysql的,结果今晚发现这一篇文章——用尽洪荒之力整理的Mysql数据库32条军规,和我的竟有异曲同工之妙,当然不同 ...

  9. Oracle undo 表空间管理 (摘DAVID)

    Oracle 的Undo有两种方式: 一是使用undo 表空间,二是使用回滚段. 我们通过 undo_management 参数来控制使用哪种方式,如果设为auto,就使用UNDO 表空间,这时必须要 ...

  10. node.js 学习01

    PHP开发技术栈(LAMP) Linux  Apache  MySql  PHPnode.js 全栈开发技术栈(MEAN): MongoDB  Express  Angular  Node.js 现阶 ...