C++ Standard Library (7): tuple type

This article is the reading notes of "C++ Primer"

#include <tuple>

tupleIs a similar pairtemplate. Different tupletypes of member types can be different, and tuplethere can be any number of members

The tuplenumber of members of each certain type is fixed

Definition and initializationtuple

Insert picture description here

Note: The second way to initialize each member with an initial value, because the corresponding constructor is yes explicit, so you can only use direct initialization syntax

Visiting tuplemember

Insert picture description here

To access a tuplemember, it is necessary to use a getstandard library function template named . In order to use get, we must specify an explicit template argument ( must be an integer constant expression ), which indicates which member we want to access . We pass to getan tupleobject, which returns a reference to the specified member:

auto book = get<0>(item); //返回item的第一个成员

View tuplethe number and type of members

If you don’t know an tupleaccurate type details, you can use two auxiliary class templates to query tuplethe number and type of members

Insert picture description here

auto item = make_tuple("0-999-78345-X", 3, 20. 00);
typedef decltype(item) trans; // trans 是item的类型

// 返回trans类型对象中成员的数量
size_t sz = tuple_size<trans>::value; //返回3
// cnt的类型与item中第二个成员相同
tuple_element<1, trans>::type cnt = get<1>(item); // cnt是一个int

Relation and equality operators

Insert picture description here
tupleWe can compare them only when two members have the same number

Since tuplethe <sum ==operator is defined , we can pass the tuplesequence to the algorithm, and it can be tupleused as the key type in the unordered container

Use tupleto return multiple values

tupleA common use of is to return multiple values ​​from a function . For example, our bookstore may be one of many chain bookstores. Each bookstore has a sales record file that saves the recent sales data of each book. We may want to check the sales of a certain book in all bookstores

Assume that every bookstore has a sales record file. Each file stores all sales records of each book together. Further assume that a function can already read these sales records file, create one for each of the bookstore vector<Sales_data>, and those vectorheld in vectorthe vectormiddle:

// files中的每个元素保存一家书店的销售记录
vector<vector<Sales_data>> files;

We will write a function that, for a given book, filessearch for bookstores that have sold the book. For each bookstore that has matching sales records, we will create an tupleindex and two iterators to store this bookstore. The index points out the filesposition of the bookstore in , and the two iterators mark the position of the given book after the vector<Sales_ data>first sales record and the last sales record in this bookstore


We first write a function to find a given book:

// matches 有三个成员: 一家书店的索引和两个指向书店vector 中元素的迭代器
typedef tuple<vector<Sales_data>::size_type,
			vector<Sales_data>::const_iterator,
			vector<Sales_data>::const_iterator> matches;
			
// files 保存每家书店的销售记录,book 表示要查找的书籍的ISBN号
// findBook 返回一个vector, 每家销售了给定书籍的书店在其中都有一项
vector<matches>
findBook(const vector<vector<Sales_data>> &files, const string &book)
{
    
    
	vector<matches> ret; 
	// 对每家书店, 查找与给定书籍匹配的记录范围(如果存在的话)
	for (auto it = files.cbegin(); it != files.cend(); ++it) {
    
    
		// 查找具有相同ISBN 的Sales_data 范围
		auto found = equal_range(it->cbegin(), it->cend(), book, compareisbn);
		if (found.first != found.second) // 此书店销售了给定书籍
			// 记住此书店的索引及匹配的范围
			ret.push_back(make_tuple(it - files.cbegin(),
										found.first, found.second));
	}
	return ret; // 如果未找到匹配记录的话, ret 为空
}

Guess you like

Origin blog.csdn.net/weixin_42437114/article/details/109270163