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

std::tuple_element<std::pair>

Defined in header <utility>

template< class T1, class T2 > struct tuple_element<0, std::pair<T1,T2> >;

(1)

(since C++11)

template< class T1, class T2 > struct tuple_element<1, std::pair<T1,T2> >;

(2)

(since C++11)

的部分专门性std::tuple_element对于对,使用类似于元组的语法提供对对%27s元素类型的编译时访问。

成员类型

第一版

*。

成员类型定义

T1型

第二版

成员类型定义

T2型

可能的实施

模板<std::size[医]TI,type Name T>struct tuple[医]元素;模板<type Name t1,type Name t2>structtuple[医]元素<0,std::偶数<t1,t2>{使用type=t1;};模板<type name t1,type Name t2>struct tuple[医]元素<1,std::对<t1,t2>>{使用类型=t2;};

*。

二次

代码语言:javascript
复制
#include <tuple>
#include <iostream>
#include <string>
 
template <int N, typename T, typename U>
static auto constexpr get(std::pair<T, U> const& pair)
    -> typename std::tuple_element<N, decltype(pair)>::type
{
    return N == 0 ? pair.first : pair.second;
}
 
int main()
{
    auto var = std::make_pair(1, std::string{"one"});
 
    std::cout << get<0>(var) << " = " << get<1>(var);
}

二次

产出:

二次

代码语言:javascript
复制
1 = one

二次

另见

tuple_element

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

std::tuple_element<std::array>

obtains the type of the elements of array (class template specialization)

std::tuple_size<std::pair> (C++11)

obtains the size of a pair (class template specialization)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券