function template (函数模板)
<utility>

std::get (pair)

lvalue (1) (左值 (1))
template <size_t I, class T1, class T2>  typename tuple_element< I, pair<T1,T2> >::type&  get (pair<T1,T2>&  pr) noexcept;
rvalue (2) (右值 (2))
template <size_t I, class T1, class T2>  typename tuple_element< I, pair<T1,T2> >::type&& get (pair<T1,T2>&& pr) noexcept;
const (3) (常量 (3))
template <size_t I, class T1, class T2>  const typename tuple_element< I, pair<T1,T2> >::type&    get (const pair<T1,T2>& pr) noexcept;
Get element (tuple interface) (获取元素 (元组接口))
Returns a reference to member first if I is 0, or a reference to member second if I is 1. (如果 I0,返回成员 first 的引用;如果 I1,返回成员 second 的引用。)

This overload of tuple's homonym function get is provided so that pair objects can be treated as a tuples. For that purpose, header <utility> also overloads tuple_size and tuple_element types with the appropriate members defined. (提供 tuple 的同名函数 get 的这种重载,以便 pair 对象可以被视为 元组。为此,头文件 <utility> 还会使用定义的相应成员重载 tuple_sizetuple_element 类型。)

模板参数

I (索引)
Position of an element in the pair, with 0 identifying member first, and 1 identifying member second. ( pair 中元素的位置,0 表示成员 first1 表示成员 second。)
size_t 是一个无符号整数类型。
T1, T2
pair 中元素的类型。

Function parameters (函数参数)

pr
A pair object. (一个 pair 对象。)

返回值

A reference to a member of the pair. (对 pair 的成员的引用。)
For rvalue pair objects (2), the function returns an rvalue reference (as if forward was used). (对于 右值 pair 对象 (2),该函数返回一个 右值引用 (就像使用了 forward 一样)。)

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// accessing pairs with get
#include <utility>      // std::pair, std::get
#include <iostream>     // std::cout

int main () {
  std::pair <int,char> foo (10,'x');

  std::get<0>(foo) = 50;

  std::cout << "foo contains: ";
  std::cout << std::get<0>(foo) << " and " << std::get<1>(foo) << '\n';

  return 0;
}

输出
foo contains: 50 and x


数据竞争

One of the members of pr is potentially accessed or modified by the caller. Concurrently accessing the other is safe. (调用者可能会访问或修改 pr 的其中一个成员。并发访问另一个成员是安全的。)

异常安全

无异常保证:此函数从不抛出异常。

另见