C++11 tuple

C++11 tuple(元组)

tuple是C++11中引入的新类型,中文翻译就是元组。元组是一个能够容纳元素集合的对象。 每个元素可以具有不同的类型。类模板std :: tuple是固定大小的异构值集合。具体可以参考tuple-cppreference.comtuple-cplusplus.com中的描述,并且提供了一些示例程序。
在使用tuple之前需要导入对应的头文件:#include
tuple的原型声明如下:

template< class... Types >
class tuple; (C++11)

其中,tuple-cplusplus.com中提供的示例程序如下:

// 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

C++中使用tuple和pair可以返回多个不同类型的多个值。示例程序如下:

#include<bits/stdc++.h> 
using namespace std; 
  
// A Method that returns multiple values using 
// tuple in C++. 
tuple<int, int, char> foo(int n1, int n2) 
{ 
    // Packing values to return a tuple 
    return make_tuple(n2, n1, 'a');              
} 
  
// A Method returns a pair of values using pair 
std::pair<int, int> foo1(int num1, int num2) 
{ 
    // Packing two values to return a pair  
    return std::make_pair(num2, num1);             
} 
  
int main() 
{ 
    int a,b; 
    char cc; 
      
    // Unpack the elements returned by foo 
    tie(a, b, cc) = foo(5, 10);       
      
    // Storing  returned values in a pair  
    pair<int, int> p = foo1(5,2);   
      
    cout << "Values returned by tuple: "; 
    cout << a << " " << b << " " << cc << endl; 
      
    cout << "Values returned by Pair: "; 
    cout << p.first << " " << p.second; 
    return 0; 
} 

Output

Values returned by tuple: 10 5 a
Values returned by Pair: 2 5

C++中的tuple

什么是tuple元组?

tuple是可以容纳许多元素的独享。元素可以是不同的数据类型。元组中的元素被初始化为参数,以便可以访问它们。

元组上的操作

  • 1.get() : - get()用于访问元组值并对其进行修改,它接受索引和元组名称作为访问特定元组元素的参数。
  • 2.make_tuple() : -make_tuple()用于为元组分配值。传递的值应与在tuple中声明的值一致。
// C++ code to demonstrate tuple, get() and make_pair() 
#include<iostream> 
#include<tuple> // for tuple 
using namespace std; 
int main() 
{ 
	// Declaring tuple 
	tuple <char, int, float> geek; 

	// Assigning values to tuple using make_tuple() 
	geek = make_tuple('a', 10, 15.5); 

	// Printing initial tuple values using get() 
	cout << "The initial values of tuple are : "; 
	cout << get<0>(geek) << " " << get<1>(geek); 
	cout << " " << get<2>(geek) << endl; 

	// Use of get() to change values of tuple 
	get<0>(geek) = 'b'; 
	get<2>(geek) = 20.5; 

	// Printing modified tuple values 
	cout << "The modified values of tuple are : "; 
	cout << get<0>(geek) << " " << get<1>(geek); 
	cout << " " << get<2>(geek) << endl; 

	return 0; 
} 

输出:

The initial values of tuple are : a 10 15.5
The modified values of tuple are : b 10 20.5

在以上代码中,get()修改tuple的第1个和第3个值。

  • 3.tuple_size :-返回tuple中存在的元素数。
//C++ code to demonstrate tuple_size 
#include<iostream> 
#include<tuple> // for tuple_size and tuple 
using namespace std; 
int main() 
{ 

	// Initializing tuple 
	tuple <char,int,float> geek(20,'g',17.5); 

	// Use of size to find tuple_size of tuple 
	cout << "The size of tuple is : "; 
	cout << tuple_size<decltype(geek)>::value << endl; 

	return 0; 

} 

输出:

The size of tuple is : 3
  • 4.swap() :-swap()交换两个不同的元组中的元素。
//C++ code to demonstrate swap() 
#include<iostream> 
#include<tuple> // for swap() and tuple 
using namespace std; 
int main() 
{ 

	// Initializing 1st tuple 
	tuple <int,char,float> tup1(20,'g',17.5); 
	
	// Initializing 2nd tuple 
	tuple <int,char,float> tup2(10,'f',15.5); 
	
	// Printing 1st and 2nd tuple before swapping 
	cout << "The first tuple elements before swapping are : "; 
	cout << get<0>(tup1) << " " << get<1>(tup1) << " "
		<< get<2>(tup1) << endl; 
	cout << "The second tuple elements before swapping are : "; 
	cout << get<0>(tup2) << " " << get<1>(tup2) << " "
		<< get<2>(tup2) << endl; 
	
	// Swapping tup1 values with tup2 
	tup1.swap(tup2); 
	
	// Printing 1st and 2nd tuple after swapping 
	cout << "The first tuple elements after swapping are : "; 
	cout << get<0>(tup1) << " " << get<1>(tup1) << " "
		<< get<2>(tup1) << endl; 
	cout << "The second tuple elements after swapping are : "; 
	cout << get<0>(tup2) << " " << get<1>(tup2) << " "
		<< get<2>(tup2) << endl; 

	return 0; 
} 

输出:

The first tuple elements before swapping are : 20 g 17.5
The second tuple elements before swapping are : 10 f 15.5
The first tuple elements after swapping are : 10 f 15.5
The second tuple elements after swapping are : 20 g 17.5
    1. tie() :- tie()的工作是将元组值解包到单独的变量中。 tie()有两种变体,有和没有“ ignore”,“ ignore”将忽略特定的元组元素并阻止其解压缩。
// C++ code to demonstrate working of tie() 
#include<iostream> 
#include<tuple> // for tie() and tuple 
using namespace std; 
int main() 
{ 
	// Initializing variables for unpacking 
	int i_val; 
	char ch_val; 
	float f_val; 
	
	// Initializing tuple 
	tuple <int,char,float> tup1(20,'g',17.5); 

	// Use of tie() without ignore 
	tie(i_val,ch_val,f_val) = tup1; 
	
	// Displaying unpacked tuple elements 
	// without ignore 
	cout << "The unpacked tuple values (without ignore) are : "; 
	cout << i_val << " " << ch_val << " " << f_val; 
	cout << endl; 
	
	// Use of tie() with ignore 
	// ignores char value 
	tie(i_val,ignore,f_val) = tup1; 
	
	// Displaying unpacked tuple elements 
	// with ignore 
	cout << "The unpacked tuple values (with ignore) are : "; 
	cout << i_val << " " << f_val; 
	cout << endl; 

	return 0; 

} 

输出:

The unpacked tuple values (without ignore) are : 20 g 17.5
The unpacked tuple values (with ignore) are : 20 17.5
    1. tuple_cat() :- 此函数连接两个元组并返回一个新的元组。
// C++ code to demonstrate working of tuple_cat() 
#include<iostream> 
#include<tuple> // for tuple_cat() and tuple 
using namespace std; 
int main() 
{ 
	// Initializing 1st tuple 
	tuple <int,char,float> tup1(20,'g',17.5); 

	// Initializing 2nd tuple 
	tuple <int,char,float> tup2(30,'f',10.5); 
	
	// Concatenating 2 tuples to return a new tuple 
	auto tup3 = tuple_cat(tup1,tup2); 
	
	// Displaying new tuple elements 
	cout << "The new tuple elements in order are : "; 
	cout << get<0>(tup3) << " " << get<1>(tup3) << " "; 
	cout << get<2>(tup3) << " " << get<3>(tup3) << " "; 
	cout << get<4>(tup3) << " " << get<5>(tup3) << endl; 

	return 0; 
} 

输出:

The new tuple elements in order are : 20 g 17.5 30 f 10.5

参考资料

猜你喜欢

转载自blog.csdn.net/ccf19881030/article/details/105650578