天天看点

c++里对组pair的使用

//对组pair:对组中的两个元素的数据类型可以一样,也可以不一样

void test_pair() {

	pair<string, int> p1((string)"prim", 23);         //1,使用拷贝构造方式创建
	
	cout << "name : " << p1.first << "   ";           //first获取对组中的第一个元素
	cout << "age : " << p1.second << endl;            //second获取对组中的第二个元素

	pair<string, int> p2 = make_pair("binary", 22);  //2.使用make_pair方式创建对组

	cout << "name : " << p2.first << "   ";
	cout << "age : " << p2.second << endl;

	p2.swap(p1);                                     //交换两个拥有相同数据类型的对组
	cout << "name : " << p2.first << "   ";
	cout << "age : " << p2.second << endl;

	pair<string, int> p3 = p1;                       //3.重载 =
	cout << "name : " << p3.first << "   ";
	cout << "age : " << p3.second << endl;
}
           

继续阅读