std::tuple
来自cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
| Defined in header <tuple>
|
||
| template< class... Types > class tuple; |
(C++11 起) | |
类模板
std::tuple的异构值是一个固定大小的集合。这是一个概括的std::pair.Original:
Class template
std::tuple is a fixed-size collection of heterogeneous values. It is a generalization of std::pair.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
目录 |
[编辑] 成员函数
| 构造一个新的 tuple Original: constructs a new tuple The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
| 的内容赋给一个 tuple到另一个Original: assigns the contents of one tuple to anotherThe text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
| 掉期2 tuples的内容 Original: swaps the contents of two tuples The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
[编辑] 非成员函数
| 创建一个 tuple对象的参数类型定义的类型Original: creates a tuple object of the type defined by the argument typesThe text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数模板) | |
| 创建一个 tuple的左值引用或解压缩到单个对象的元组Original: creates a tuple of lvalue references or unpacks a tuple into individual objectsThe text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数模板) | |
| 创建一个 tuple的右值引用Original: creates a tuple of rvalue referencesThe text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数模板) | |
| 创建一个 tuple通过连接任意数量的元组Original: creates a tuple by concatenating any number of tuplesThe text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数模板) | |
| tuple访问指定的元素 Original: tuple accesses specified element The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数模板) | |
| 字典在数组中的值进行比较 Original: lexicographically compares the values in the tuple The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数模板) | |
| (C++11) |
专业的std::swap算法 Original: specializes the std::swap algorithm The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数模板) |
[编辑] Helper类
| 在编译时获得的大小 tuple Original: obtains the size of tuple at compile time The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (类模板专业化) | |
| 获得指定的元素的类型 Original: obtains the type of the specified element The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (类模板专业化) | |
| 专业的std::uses_allocator型特征 Original: specializes the std::uses_allocator type trait The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (类模板专业化) | |
| 占位符时跳过元素拆开一个 tupletie Original: placeholder to skip an element when unpacking a tuple using tie The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (不变) | |
[编辑] 为例
#include <tuple> #include <iostream> #include <string> #include <stdexcept> std::tuple<double, char, std::string> get_student(int id) { if (id == 0) return std::make_tuple(3.8, 'A', "Lisa Simpson"); if (id == 1) return std::make_tuple(2.9, 'C', "Milhouse Van Houten"); if (id == 2) return std::make_tuple(1.7, 'D', "Ralph Wiggum"); throw std::invalid_argument("id"); } int main() { auto student0 = get_student(0); std::cout << "ID: 0, " << "GPA: " << std::get<0>(student0) << ", " << "grade: " << std::get<1>(student0) << ", " << "name: " << std::get<2>(student0) << '\n'; double gpa1; char grade1; std::string name1; std::tie(gpa1, grade1, name1) = get_student(1); std::cout << "ID: 1, " << "GPA: " << gpa1 << ", " << "grade: " << grade1 << ", " << "name: " << name1 << '\n'; }
Output:
ID: 0, GPA: 3.8, grade: A, name: Lisa Simpson ID: 1, GPA: 2.9, grade: C, name: Milhouse Van Houten