[STL基础]pair组对单位模板类

pair模板类用来绑定两个对象为一个新的对象,该类型在<utility>头文件中定义
pair模板类支持如下操作

  • pair<T1,T2> p1:创建一个空的pair对象,它的两个元素分别是T1和T2类型,采用值初始化
  • pari<T1,T2> p1(v1,v2):创建一个pair对象,它的两个元素分别是T1和T2类型,其中first成员初始化为v1,second成员初始化为v2
  • make_pair(v1,v2):以v1和v2值创建一个新的pair对象,其元素类型分别是v1和v2的类型
  • p1<p2字典次序:如果p1.first<p2.first或者(p1.first>p2.first)&&(p1.second<p2.second),则返回true
  • p1==p2:如果两个pair对象的first和second成员依次相等,则这两个对象相等
  • p.first/p.second:返回p中的名为first/second的公有数据成员
 //pair:对组,可以将两个值(first,second)视为一个单元(pair),是个模板类。对于map/multimap,就是用pairs来管理value/key的成对元素。任何函数需要回传两个值,也需要pair
#include <utility>
#include <string>
#include <conio.h>
#include<iostream>
using namespace std;    
void test0()
{   
	pair<string,double> product1("tomatoes",3.25);//定义一个组单元
	pair<string,double> product2;
	pair<string,double> product3;
	product2.first="lightbulbs";
	product2.second=0.99;//设置pair的first,second数据
	product3=make_pair("shoes",20.0);//make_pair是个模板函数,返回pair
	cout<<"the price of "<<product1.first<<" is $"<<product1.second<<endl;//获取pair的first,second的数据
	cout<<"the price of "<<product2.first<<" is $"<<product2.second<<endl;
	cout<<"the price of "<<product3.first<<" is $"<<product3.second<<endl;
} 
void Test(char h)
{
	cout<<"press key===="<<h<<endl;
	switch(h)
	{ 
	case '0':  test0();break; 
	case 27:
	case 'q':exit(0);break; 
	default:cout<<"default "<<h<<endl;break;
	}
}
void main()
{
	while(1)
	{
		Test(getch());
	} 
}


网上实例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
STL pair 是 C++ STL(标准模板库)中的一个模板类,用于存储两个不同类型的数据。它的定义如下: ``` template <class T1, class T2> struct pair { typedef T1 first_type; typedef T2 second_type; T1 first; T2 second; // 构造函数 pair(); pair(const T1& x, const T2& y); template<class U, class V> pair(const pair<U, V>& p); // 操作符重载 pair& operator=(const pair& p); template<class U, class V> pair& operator=(const pair<U, V>& p); void swap(pair& p); }; ``` 其中,`first_type` 和 `second_type` 分别表示 `pair` 的两个元素的类型。`first` 和 `second` 分别表示 `pair` 的两个元素的值。`pair` 的构造函数可以用来初始化两个元素的值,而操作符重载可以用来进行两个 `pair` 对象的比较和赋值。 下面是一个使用 `pair` 的例子: ```cpp #include <iostream> #include <utility> using namespace std; int main() { pair<string, int> p1("Hello", 123); cout << "p1.first = " << p1.first << endl; cout << "p1.second = " << p1.second << endl; pair<string, int> p2 = make_pair("World", 456); cout << "p2.first = " << p2.first << endl; cout << "p2.second = " << p2.second << endl; if (p1 < p2) { cout << "p1 is less than p2" << endl; } else { cout << "p1 is greater than or equal to p2" << endl; } p1 = p2; cout << "After assignment, p1.first = " << p1.first << endl; cout << "After assignment, p1.second = " << p1.second << endl; return 0; } ``` 输出结果为: ``` p1.first = Hello p1.second = 123 p2.first = World p2.second = 456 p1 is less than p2 After assignment, p1.first = World After assignment, p1.second = 456 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值