构造

  构造函数

   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. 用正则把url解析为对象

    用正则把url解析为对象 <!DOCTYPE html><html><head><meta charset="utf-8">< ...

  2. Linq 简明教程

    一个简单的实例 static void Main(string[] args) { string[] names = { "Alonso", "Zheng", ...

  3. css3动画和JS+DOM动画和JS+canvas动画比较

    css3兼容:IE10+.FF.oprea(animation):safari.chrome(-webkit-animation) js+dom:没有兼容问题: js+canvas:IE9+:(性能最 ...

  4. [Git] 根据commiter过滤该用户的所有提交

    git log --pretty=oneline --author="xxxx" -(n) 仅显示最近的 n 条提交 --since,--after 仅显示指定时间之后的提交 -- ...

  5. win8 下脚本安装IIS

    @echo off      echo 正在添加IIS8.0 功能,依据不同的网络速率,全程大约需要5分钟时间...      start /w pkgmgr /iu:IIS-WebServerRol ...

  6. unity GPU bound or CPU bound

    unity判断GPU CPUbound android 用unity profiler 里面的cpu时间 xcode有直接的显示

  7. uber shader

    shader 合在一起 用一些宏来控制 选哪部分编成一个想要的shader https://docs.unity3d.com/Manual/SL-MultipleProgramVariants.htm ...

  8. LoadRunner中如何验证下载的文件大小、统计下载时间、度量下载速度

    LoadRunner中的web_get_in_property函数可用于返回上一个HTTP请求的相关信息,包括HTTP请求返回码.下载大小.下载时间等: The web_get_int_propert ...

  9. C/C++获取当前系统时间

    个人觉得第二种还是比较实用的,而且也是最常用的~ 不过当计算算法耗时的时候,不要忘记second,不能只要用Milliseconds来减,不然后出现负值,若是算法耗时太长就得用minutes啦.再不然 ...

  10. thread::id

    线程标识符id可以通过thread::get_id()获得,若thread obejct没有和任何线程关联则返回一个NULL的std::thread::id表示没有任何线程.当前线程若想获得自己的id ...