在线文档教程
C++
应用 | Utilities

std::get(std::pair)

STD::获取%28 std::对%29

Defined in header
(1)
template< size_t I, class T1, class T2 > typename std::tuple_element<I, std::pair<T1,T2> >::type& get( pair<T1, T2>& p (since C++11) (until C++14)
template< size_t I, class T1, class T2 > constexpr std::tuple_element_t<I, std::pair<T1,T2> >& get( pair<T1, T2>& p (since C++14)
(2)
template< size_t I, class T1, class T2 > const typename std::tuple_element<I, std::pair<T1,T2> >::type& get( const pair<T1,T2>& p (since C++11) (until C++14)
template< size_t I, class T1, class T2 > constexpr const std::tuple_element_t<I, std::pair<T1,T2> >& get( const pair<T1,T2>& p (since C++14)
(3)
template< size_t I, class T1, class T2 > typename std::tuple_element<I, std::pair<T1,T2> >::type&& get( std::pair<T1,T2>&& p (since C++11) (until C++14)
template< size_t I, class T1, class T2 > constexpr std::tuple_element_t<I, std::pair<T1,T2> >&& get( std::pair<T1,T2>&& p (since C++14)
template< size_t I, class T1, class T2 > constexpr const std::tuple_element_t<I, std::pair<T1,T2> >&& get( const std::pair<T1,T2>&& p (4)(since C++17)
template <class T, class U> constexpr T& get(std::pair<T, U>& p(5)(since C++14)
template <class T, class U> constexpr const T& get(const std::pair<T, U>& p(6)(since C++14)
template <class T, class U> constexpr T&& get(std::pair<T, U>&& p(7)(since C++14)
template <class T, class U> constexpr const T&& get(const std::pair<T, U>&& p(8)(since C++17)
template <class T, class U> constexpr T& get(std::pair<U, T>& p(9)(since C++14)
template <class T, class U> constexpr const T& get(const std::pair<U, T>& p(10)(since C++14)
template <class T, class U> constexpr T&& get(std::pair<U, T>&& p(11)(since C++14)
template <class T, class U> constexpr const T&& get(const std::pair<U, T>&& p(12)(since C++17)

使用类似元组的接口从一对中提取元素。

基于索引的重载%281-4%29如果索引失败,则无法编译I既不是0也不是1。

基于类型的重载%285-12%29如果类型失败,则无法编译TU都是一样的。

参数

p-pair whose contents to extract

返回值

1-4%29返回对p.first如果I==0以及引用p.second如果I==1...

5-8%29返回对p.first...

9-12%29返回对p.second...

例外

noexcept规格:

noexcept

二次

#include <iostream> #include <utility> int main() { auto p = std::make_pair(1, 3.14 std::cout << '(' << std::get<0>(p) << ", " << std::get<1>(p) << ")\n"; std::cout << '(' << std::get<int>(p) << ", " << std::get<double>(p) << ")\n"; }

二次

产出:

二次

(1, 3.14) (1, 3.14)

二次

另见

std::get(std::tuple)tuple accesses specified element (function template)
std::get(std::array)accesses an element of an array (function template)
std::get(std::variant) (C++17)reads the value of the variant given the index or the type (if the type is unique), throws on error (function template)

© cppreference.com

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

http://en.cppreference.com/w/cpp/实用程序/对/get