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 的其中一個成員。併發訪問另一個成員是安全的。)

異常安全

無異常保證:此函式從不丟擲異常。

另見