This article is the reading notes of "C++ Primer"
table of Contents
#include <tuple>
tuple
Is a similar pair
template. Different tuple
types of member types can be different, and tuple
there can be any number of members
The
tuple
number of members of each certain type is fixed
Definition and initializationtuple
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 tuple
member
To access a tuple
member, it is necessary to use a get
standard 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 get
an tuple
object, which returns a reference to the specified member:
auto book = get<0>(item); //返回item的第一个成员
View tuple
the number and type of members
If you don’t know an tuple
accurate type details, you can use two auxiliary class templates to query tuple
the number and type of members
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
tuple
We can compare them only when two members have the same number
Since
tuple
the<
sum==
operator is defined , we can pass thetuple
sequence to the algorithm, and it can betuple
used as the key type in the unordered container
Use tuple
to return multiple values
tuple
A 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 vector
held in vector
the vector
middle:
// files中的每个元素保存一家书店的销售记录
vector<vector<Sales_data>> files;
We will write a function that, for a given book, files
search for bookstores that have sold the book. For each bookstore that has matching sales records, we will create an tuple
index and two iterators to store this bookstore. The index points out the files
position 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 为空
}