tuple元组(C++11及以后,如C++14)

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

本文主要举例:

tuple_size

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// 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 and change its value

  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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 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 typedefinition
type The Ith type in the tuple object

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 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 constantdefinition
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 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
posted @   PKICA  阅读(504)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示