C++黑马程序员——P228. pair对组

发布时间 2023-08-15 14:46:56作者: 我会变强的
  • P228. pair使用——pair对组的创建

功能描述:

  成对出现的数据,利用对组可以返回两个数据

两种创建方式:

pair<type, type>p(value1, value2);

pair<type, type>p = make_pair(value1, value2);

#include <iostream>
#include <string>
using namespace std;

// pair对组创建
void test01() {
	// 第一种方式创建
	pair<string, int>p("Tom", 20);
        // ★★★对组访问★★★
	cout << "姓名:" << p.first << " 年龄:" << p.second << endl;
	// 第二种方式
	pair<string, int>p2 = make_pair("Jerry", 30);
	cout << "姓名:" << p2.first << " 年龄:" << p2.second << endl;
}

int main() {
	test01();
	return 0;
}

res:

  

(〃>_<;〃)(〃>_<;〃)(〃>_<;〃)