이름공간
변수
행위

std::tuple

cppreference.com
< cpp‎ | utility
Defined in header <tuple>
template< class... Types >
class tuple;
(since C++11)

클래스 템플릿 std::tuple 은 여러 종류의 값에 대한 고정 크기 컬렉션 입니다. std::pair의 일반화된 형태입니다.

목차

[편집] Template parameters

Types... - tuple에 저장될 요소의 타입들. 빈 리스트도 지원합니다.

[편집] Member functions

새로운 tuple을 생성합니다.
(public member function)
어떤 tuple의 내용을 다른 튜플에 할당합니다.
(public member function)
tuple의 내용을 교환합니다.
(public member function)

[편집] Non-member functions

creates a tuple object of the type defined by the argument types
(function template) [edit]
creates a tuple of lvalue references or unpacks a tuple into individual objects
(function template) [edit]
creates a tuple of rvalue references
(function template) [edit]
creates a tuple by concatenating any number of tuples
(function template) [edit]
tuple accesses specified element
(function template) [edit]
lexicographically compares the values in the tuple
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]

[편집] Helper classes

obtains the size of tuple at compile time
(class template specialization) [edit]
obtains the type of the specified element
(class template specialization) [edit]
specializes the std::uses_allocator type trait
(class template specialization) [edit]
placeholder to skip an element when unpacking a tuple using tie
(constant) [edit]

[편집] Example

#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