码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-10-12 22:17:28      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:

类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

  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

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

标签:

原文地址:http://www.cnblogs.com/guxuanqing/p/4872798.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!