#include <iostream>
#include <utility> // 包含 pair 的头文件
using namespace std;
int main() {
// 创建一个 pair,存储两个不同类型的数据
pair<int, string> myPair(10, "Hello");
// 访问 pair 中的元素
cout << "First element: " << myPair.first << endl; // 输出 10
cout << "Second element: " << myPair.second << endl; // 输出 Hello
// 修改 pair 中的元素
myPair.first = 20;
myPair.second = "World";
// 再次访问修改后的元素
cout << "Modified first element: " << myPair.first << endl; // 输出 20
cout << "Modified second element: " << myPair.second << endl; // 输出 World
return 0;
}
pair
是 C++ 标准库中的一种模板类,位于 <utility>
头文件中。它可以存储两个不同类型的对象。pair<int, string> myPair(10, "Hello");
创建了一个包含整数和字符串的 pair
对象。myPair.first
和 myPair.second
分别用于访问 pair
中的第一个和第二个元素。first
和 second
成员进行赋值操作,以修改 pair
中存储的值。上一篇:c++ decltype
下一篇:c++ lower_bound
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站