类tuple与array最本质的区别当数tuple元组元素类型可以不一样,而统一数组array的元素类型必须一样。

本文主要举例:

tuple_size

Example

123456789101112131415161718192021222324252627
// tuple example
#include <iostream>     // std::cout
#include <tuple>        // std::tuple, std::get, std::tie, std::ignore

int main ()
{
  std::tuple<int,char> foo (10,'x');
  auto bar = std::make_tuple ("test", 3.1, 14, 'y');

  std::get<2>(bar) = 100;                                    // access element

  int myint; char mychar;

  std::tie (myint, mychar) = foo;                            // unpack elements
  std::tie (std::ignore, std::ignore, myint, mychar) = bar;  // unpack (with ignore)

  mychar = std::get<3>(bar);

  std::get<0>(foo) = std::get<2>(bar);
  std::get<1>(foo) = mychar;

  std::cout << "foo contains: ";
  std::cout << std::get<0>(foo) << ' ';
  std::cout << std::get<1>(foo) << '\n';

  return 0;
}

Output:

foo contains: 100 y

tie----(unpack)

Example

1234567891011121314151617181920
// packing/unpacking tuples
#include <iostream>     // std::cout
#include <tuple>        // std::tuple, std::make_tuple, std::tie

int main ()
{
  int myint;
  char mychar;

  std::tuple<int,float,char> mytuple;

  mytuple = std::make_tuple (10, 2.6, 'a');          // packing values into tuple

  std::tie (myint, std::ignore, mychar) = mytuple;   // unpacking tuple into variables

  std::cout << "myint contains: " << myint << '\n';
  std::cout << "mychar contains: " << mychar << '\n';

  return 0;
}

Output:

myint contains: 10
mychar contains: a

std:tuple_element

Member types

member type definition
type The Ith type in the tuple object

Example

123456789101112131415
// tuple_element
#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 << '\n';

  return 0;
}

Output:

mytuple contains: 10 and a
tuple_size

Member constants

member constant definition
value The number of elements in the tuple or tuple-like object.
This is a constexpr value of the unsigned integral type size_t.

Example

1234567891011121314
// 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." << '\n';

  return 0;
}

Output:

mytuple has 3 elements
forward_as_tuple

Parameters

args
List of elements to be forwarded as a tuple object of references.

Return Value

tuple object with rvalue references to args suitable to be forwarded as argument to a function.

Example

123456789101112131415
// forward_as_tuple example
#include <iostream>     // std::cout
#include <tuple>        // std::tuple, std::get, std::forward_as_tuple
#include <string>       // std::string

void print_pack (std::tuple<std::string&&,int&&> pack) {
  std::cout << std::get<0>(pack) << ", " << std::get<1>(pack) << '\n';
}

int main() {
  std::string str ("John");
  print_pack (std::forward_as_tuple(str+" Smith",25));
  print_pack (std::forward_as_tuple(str+" Daniels",22));
  return 0;
}

Output:

John Smith, 25
John Daniels, 22
tuple_cat

Parameters

tpls
Comma-separated list of tuple objects. These may be of different types.

Return Value

tuple object of the appropriate type to hold args.

The type of the returned object (tuple<CTypes...>) is the tuple type that contains the ordered sequence of all the extended types inTuples.

Example

12345678910111213141516171819202122
// tuple_cat example
#include <iostream>     // std::cout
#include <utility>      // std::pair
#include <string>       // std::string
#include <tuple>        // std::tuple, std::tuple_cat, std::get

int main ()
{

  std::tuple<float,std::string> mytuple (3.14,"pi");
  std::pair<int,char> mypair (10,'a');

  auto myauto = std::tuple_cat ( mytuple, std::tuple<int,char>(mypair) );

  std::cout << "myauto contains: " << '\n';
  std::cout << std::get<0>(myauto) << '\n';
  std::cout << std::get<1>(myauto) << '\n';
  std::cout << std::get<2>(myauto) << '\n';
  std::cout << std::get<3>(myauto) << '\n';

  return 0;
}

Output:

myauto contains:
3.14
pi
10
a

ignore tie

Parameters

args
List of objects (lvalues) to be tied as elements of a tuple.

Return Value

tuple with lvalue references to args.

Example

1234567891011121314151617181920
// packing/unpacking tuples
#include <iostream>     // std::cout
#include <tuple>        // std::tuple, std::make_tuple, std::tie

int main ()
{
  int myint;
  char mychar;

  std::tuple<int,float,char> mytuple;

  mytuple = std::make_tuple (10, 2.6, 'a');          // packing values into tuple

  std::tie (myint, std::ignore, mychar) = mytuple;   // unpacking tuple into variables

  std::cout << "myint contains: " << myint << '\n';
  std::cout << "mychar contains: " << mychar << '\n';

  return 0;
}

Output:

myint contains: 10
mychar contains: a

tuple元组(C++11及以后,如C++14)的更多相关文章

  1. C++11 tuple元组

    C++11 tuple 元组 tuple容器(元组), 是表示元组容器, 是不包含任何结构的,快速而低质(粗制滥造, quick and dirty)的, 可以用于函数返回多个返回值; tuple容器 ...

  2. Python - 基础数据类型 tuple 元组

    元组简单介绍 元组是一个和列表和相似的数据类型,也是一个有序序列 两者拥有着基本相同的特性,但是也有很多不同的地方 声明元组 var = (1, 2, 3) var = ("1", ...

  3. Tuple元组

    Tuple元组 Tuple 是 Storm 的主要数据结构,并且是 Storm 中使用的最基本单元.数据模型和元组. Tuple 描述 Tuple 就是一个值列表, Tuple 中的值可以是任何类型的 ...

  4. 运行期以索引获取tuple元素-C++11之2

    //运行期以索引获取tuple元素-C++11之2 //需支持C++11及以上标准的编译器,VS2017 15.5.x.CodeBlocks 16.01 gcc 7.2 //参见<深入应用C++ ...

  5. 运行期以索引获取tuple元素-C++11之1

    //运行期以索引获取tuple元素-C++11之1 //需支持C++11及以上标准的编译器,VS2017 15.5.x.CodeBlocks 16.01 gcc 7.2 //参见<深入应用C++ ...

  6. Python数据类型的内置函数之tuple(元组),dict(字典),set(集合)

    Python数据类型内置函数 - str(字符串) - list(列表) - tuple(元组) - dict(字典) - set(收集) tuple(元组)的操作 - (count)统计元组中元素出 ...

  7. 列表 list 容器类型数据(str字符串, list列表, tuple元组, set集合, dict字典)--->元组 tuple-->字符串 str

    # ### 列表 list 容器类型数据(str字符串, list列表, tuple元组, set集合, dict字典) # (1)定义一个列表 listvar = [] print(listvar, ...

  8. Tuple元组 、 ValueTuple 值元组详解

    Tuple元组 Tuple是C# 4.0时出的新特性,.Net Framework 4.0以上版本可用. 元组是一种数据结构,具有特定数量和元素序列,与数组不同,元祖中的元素可以不同的数据类型.比如设 ...

  9. list列表、tuple元组、range常用方法总结

    list 列表(数组),是可迭代对象,列表是可变的所以列表的方法都是在列表本身更改的.里面看可以放各种数据类型的数据,可存储大量数据 连接列表可以使用 + 或 extend() a = [1, 3, ...

随机推荐

  1. Codeforces 85D Sum of Medians

    传送门 D. Sum of Medians time limit per test 3 seconds memory limit per test 256 megabytes input standa ...

  2. POJ2586Y2K Accounting Bug(贪心 + 不好想)

    Y2K Accounting Bug Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12251   Accepted: 62 ...

  3. 部署搭建 Saltstack(centos6.6)

    SaltStack介绍 官网:https://docs.saltstack.com/en/latest/ 中国saltstack用户组http://www.saltstack.cn/ 下图是它的子系统 ...

  4. IDE 集成开发环境

    集成开发环境(IDE,Integrated Development Environment )是用于提供程序开发环境的应用程序,一般包括代码编辑器.编译器.调试器和图形用户界面工具.集成了代码编写功能 ...

  5. Extjs Window用法详解

    今天我们来介绍一下Extjs中一个常用的控件Window.Window的作用是在页面中创建一个窗口,这个窗口作为容器,可以在它里面加入grid.form等控件,从而来实现更加复杂的界面逻辑. 本文的示 ...

  6. centos 配置固定ip

    centos下网络配置方法(网关.dns.ip地址配置) 来源:互联网 作者:佚名 时间:07-13 00:32:07 [大 中 小] 本文介绍了centos网络配置的方法,centos网络配置主要包 ...

  7. 菲涅尔反射(Fresnel Reflection)

    离线渲染中,通常可以用kd,ks,kt(分别代表物体的漫反射系数,镜面反射系数,透射系数)来简单地描述一个物体的基本材质,例如,我们将一个物体设置为:kd=0,ks=0.1,kt=0.9,即代表一束光 ...

  8. ASP.NET MVC4中使用Ninject

    1.NuGet获取Ninject.dll 选中项目右键: .NET技术交流群 199281001 .欢迎加入. 2.全局注册  Global.asax.cs //注册Ninject依赖注入全局解析器 ...

  9. thinkphp 3.23 第三方登录sdk集成包

    本集成包在官方包上扩展了支付宝登录和微信,支持最新的3.23版本 config    /* URL配置 */     'URL_CASE_INSENSITIVE' => true, //默认fa ...

  10. Javascript 将图片的绝对路径转换为base64编码

    Javascript将图片的绝对路径转换为base64编码 我们可以使用canvas.toDataURL的方法将图片的绝对路径转换为base64编码:在这我们引用的是淘宝首页一张图片如下: var i ...