public member function (公共成員函式)
<utility>
std::pair::pair (std::對::對)
預設 (1) | pair(); |
---|
複製 (2) | template<class U, class V> pair (const pair<U,V>& pr); |
---|
initialization (3) (初始化 (3)) | pair (const first_type& a, const second_type& b); |
---|
預設 (1) | constexpr pair(); |
---|
copy / move (2) (複製/移動 (2)) | template<class U, class V> pair (const pair<U,V>& pr);template<class U, class V> pair (pair<U,V>&& pr);pair (const pair& pr) = default;pair (pair&& pr) = default; |
---|
initialization (3) (初始化 (3)) | pair (const first_type& a, const second_type& b);template<class U, class V> pair (U&& a, V&& b); |
---|
piecewise (4) (分段 (4)) | template <class... Args1, class... Args2> pair (piecewise_construct_t pwc, tuple<Args1...> first_args, tuple<Args2...> second_args); |
---|
Construct pair (構造對)
Constructs a pair object. (構造一個pair物件。)
This involves individually constructing its two component objects, with an initialization that depends on the constructor form invoked (這涉及到單獨構造它的兩個組成物件,其初始化取決於呼叫的建構函式形式)
- (1) 預設建構函式
- Constructs a pair object with its elements value-initialized. (構造一個pair物件,其元素進行值初始化。)
- (2) copy / move constructor (and implicit conversion) ((2) 複製/移動建構函式(和隱式轉換))
- The object is initialized with the contents of the pr pair object. (該物件使用pr pair物件的內容進行初始化。)
The corresponding member of pr is passed to the constructor of each of its members. (pr的相應成員被傳遞給其每個成員的建構函式。)
- (3) initialization constructor ((3) 初始化建構函式)
- Member first is constructed with a and member second with b. (成員first使用a構造,成員second使用b構造。)
- (4) piecewise constructor ((4) 分段建構函式)
- Constructs members first and second in place, passing the elements of first_args as arguments to the constructor of first, and the elements of second_args to the constructor of second. (就地構造成員first和second,將first_args的元素作為引數傳遞給first的建構函式,並將second_args的元素傳遞給second的建構函式。)
Most forms have two signatures: one taking const lvalue references, which copies the values into the pair, and one taking rvalue references, which moves them instead if their types support move semantics (for such types, the contents are transferred to the pair object and lost by their previous referrers, which are left in an unspecified but valid state). (大多數形式都有兩個簽名:一個採用const 左值引用,它將值複製到pair中,另一個採用右值引用,如果它們的型別支援移動語義,它會移動它們(對於這種型別,內容被轉移到pair物件,並被它們之前的引用者丟失,它們處於未指定但有效的狀態)。)
引數
- pr
- Another pair object. (另一個pair物件。)
This may be an object of the same type as the object being constructed or of a pair type whose elements' types are implicitly convertible to those in the pair being constructed. (這可以是與正在構造的物件相同型別的物件,也可以是pair型別,其元素的型別可以隱式轉換為正在構造的pair中的型別。)
- a
- An object of the type of first, or some other type implicitly convertible to it. (型別為first的物件,或可隱式轉換為它的其他型別。)
- 和 b
- An object of the type of second, or some other type implicitly convertible to it. (型別為second的物件,或可隱式轉換為它的其他型別。)
- pwc
- The piecewise_construct object. (piecewise_construct物件。)
The only purpose of this argument is to select the proper constructor signature. It conveys no information to be incorporated into the new object. (此引數的唯一目的是選擇正確的建構函式簽名。它不傳遞任何要合併到新物件中的資訊。)
- first_args, second_args
- tuple objects with the arguments to be passed to the constructors of members first and second. (tuple物件,其中包含要傳遞給成員first和second的建構函式的引數。)
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
// pair::pair example
#include <utility> // std::pair, std::make_pair
#include <string> // std::string
#include <iostream> // std::cout
int main () {
std::pair <std::string,double> product1; // default constructor
std::pair <std::string,double> product2 ("tomatoes",2.30); // value init
std::pair <std::string,double> product3 (product2); // copy constructor
product1 = std::make_pair(std::string("lightbulbs"),0.99); // using make_pair (move)
product2.first = "shoes"; // the type of first is string
product2.second = 39.90; // the type of second is double
std::cout << "The price of " << product1.first << " is $" << product1.second << '\n';
std::cout << "The price of " << product2.first << " is $" << product2.second << '\n';
std::cout << "The price of " << product3.first << " is $" << product3.second << '\n';
return 0;
}
|
輸出
The price of lightbulbs is $0.99
The price of shoes is $39.9
The price of tomatoes is $2.3
|
資料競爭
The elements of pr, first_args and second_args are accessed. (訪問pr、first_args和second_args的元素。)
The constructors taking rvalue references as arguments modify these arguments if their types support move semantics for this construction. (如果它們的型別支援此構造的移動語義,則採用右值引用作為引數的建構函式會修改這些引數。)
異常安全
If none of the individual constructions of members of pair can throw, the operation never throws exceptions (no-throw guarantee). (如果pair成員的任何單個構造都不會丟擲異常,則該操作永遠不會丟擲異常(無丟擲保證)。)
Otherwise, if any of the forms taking an rvalue reference as argument is called, and at least one of the types in the pair can be constructed with move semantics, the operation may leave pr in an invalid state in case of exception (no guarantees). (否則,如果呼叫任何採用右值引用作為引數的形式,並且pair中的至少一種型別可以使用移動語義構造,則該操作可能會在發生異常時使pr處於無效狀態(不保證)。)
Otherwise, the function only implies copies and the operation produces no side effects (strong guarantee). (否則,該函式僅表示複製,並且該操作不會產生副作用(強保證)。)