首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::tuple

Defined in header <tuple>

template< class... Types > class tuple;

(since C++11)

Class template std::tuple is a fixed-size collection of heterogeneous values. It is a generalization of std::pair.

If (std::is_trivially_destructible_v<Types> && ...) is true, the destructor of tuple is trivial.

(since C++17)

Template parameters

Types...

-

the types of the elements that the tuple stores. Empty list is supported.

Member functions

(constructor)

constructs a new tuple (public member function)

operator=

assigns the contents of one tuple to another (public member function)

swap

swaps the contents of two tuples (public member function)

Non-member functions

make_tuple

creates a tuple object of the type defined by the argument types (function template)

tie

creates a tuple of lvalue references or unpacks a tuple into individual objects (function template)

forward_as_tuple

creates a tuple of rvalue references (function template)

tuple_cat

creates a tuple by concatenating any number of tuples (function template)

std::get(std::tuple)

tuple accesses specified element (function template)

operator==operator!=operator<operator<=operator>operator>=

lexicographically compares the values in the tuple (function template)

std::swap(std::tuple) (C++11)

specializes the std::swap algorithm (function template)

Helper classes

tuple_size

obtains the size of tuple at compile time (class template specialization)

tuple_element

obtains the type of the specified element (class template specialization)

std::uses_allocator<std::tuple> (C++11)

specializes the std::uses_allocator type trait (class template specialization)

ignore

placeholder to skip an element when unpacking a tuple using tie (constant)

Notes

Until C++17, a function could not return a tuple using list-initialization:

代码语言:javascript
复制
std::tuple<int, int> foo_tuple() 
{
  return {1, -1};  // Error until C++17
  return std::make_tuple(1, -1); // Always works
}

Example

代码语言:javascript
复制
#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:

代码语言:javascript
复制
ID: 0, GPA: 3.8, grade: A, name: Lisa Simpson
ID: 1, GPA: 2.9, grade: C, name: Milhouse Van Houten

References

  • C++11 standard (ISO/IEC 14882:2011):
    • 20.4 Tuples tuple
代码语言:txt
复制
 © cppreference.com

Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.

扫码关注腾讯云开发者

领取腾讯云代金券