C++ pair的定义及使用

 

声明为pair类型的变量可以有三种赋值方法:

1. 通过初始化赋值 直接声明的时候 后面加括号并且数据,如下a
2. 通过.first  .second 来赋值 如下b
3. 通过 = make_pair() 来赋值, 如下c
  a. pair <string,double> product1 ("tomatoes",3.25);
  pair <string,double> product2;
  pair <string,double> product3;
 b.  product2.first = "lightbulbs"; // type of first is string 
    product2.second = 0.99; // type of second is double


 c. product3 = make_pair ("shoes",20.0);


template <class T1, class T2> struct pair;
Pair of values

This class couples together a pair of values, which may be of different types (T1 and T2). The individual values can be accessed through the public members first and second.

The class is defined as:

1
2
3
4
5
6
7
8
9
10
11
12
template <class T1, class T2> struct pair
{
  typedef T1 first_type;
  typedef T2 second_type;

  T1 first;
  T2 second;
  pair() : first(T1()), second(T2()) {}
  pair(const T1& x, const T2& y) : first(x), second(y) {}
  template <class U, class V>
    pair (const pair<U,V> &p) : first(p.first), second(p.second) { }
}


Members

first_type, second_type
Alises of template parameters T1 and T2 respectively.
first, second
Data members containing the first and second values stored in the pair.
pair()
Constructs a pair object with each of its members first and second constructed with their respective default constructors.
pair(const T1& x, const T2& y)
Constructs a pair object with its members first and second initialized to x and y, respectively.
template <class U, class V> pair (const pair<U,V> &p)
Constructs a pair object with its members first and second initialized to the corresponding elements in p, which must be of any couple of implicitly-convertible types (including the same types).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <utility>
#include <string>
using namespace std;

int main () {
  pair <string,double> product1 ("tomatoes",3.25);
  pair <string,double> product2;
  pair <string,double> product3;

  product2.first = "lightbulbs";     // type of first is string
  product2.second = 0.99;            // type of second is double

  product3 = make_pair ("shoes",20.0);

  cout << "The price of " << product1.first << " is $" << product1.second << "\n";
  cout << "The price of " << product2.first << " is $" << product2.second << "\n";
  cout << "The price of " << product3.first << " is $" << product3.second << "\n";
  return 0;
}


Output:

The price of tomatoes is $3.25
The price of lightbulbs is $0.99
The price of shoes is $20
### 定义与初始化 `std::pair` 是 C++ 标准库中的模板类,用于存储一对具有不同类型的值。为了使用 `std::pair`,需要包含头文件 `<utility>`[^2]。 可以直接声明并初始化一个 `std::pair` 对象: ```cpp #include <iostream> #include <utility> // 包含 std::pair 和 std::make_pair // 直接指定类型创建 pair std::pair<int, const char*> p1(10, "apple"); ``` 也可以利用 `auto` 关键字让编译器自动推断类型,并借助 `std::make_pair()` 函数简化构造过程: ```cpp // 使用 make_pair 并配合 auto 推导类型 auto p2 = std::make_pair(3, "cherry"); std::cout << "First: " << p2.first << ", Second: " << p2.second << std::endl; ``` 当涉及到引用时,可以传递引用给 `std::pair` 的成员变量,这允许修改原始对象的值[^4]: ```cpp int i = 0; // 创建含有引用的 pair auto p_ref = std::make_pair(std::ref(i), std::ref(i)); p_ref.first++; p_ref.second++; if (i == 2) { std::cout << "Reference modification works as expected." << std::endl; } ``` ### 操作 `std::pair` 一旦有了 `std::pair` 实例之后,就可以通过 `.first` 成员访问第一个元素,`.second` 访问第二个元素[^1]。 除了基本的操作外,在某些情况下可能还需要比较两个 `std::pair` 是否相等或是进行排序。对于这些需求,标准库已经提供了相应的重载运算符支持[^3]。 例如,下面展示了如何对比两组数据: ```cpp bool result = (p1 == p2); if (!result) { std::cout << "Pairs are not equal." << std::endl; } else { std::cout << "Pairs are equal." << std::endl; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值