C ++程序在STL中实现配对
Pair是一个简单的容器,由两个数据对象组成:
‘first’ = The first element is referenced as ‘first’ ‘second’ = the second element and the order is fixed (first, second).
可以分配,比较和复制对。它用于将两个类型可能不同的值组合在一起。
语法为:pair<数据类型1,数据类型2>变量名称(数据值1,数据值2)。
算法
Begin Write pair<data type1,data type 2>variable name(datavalue1,datavalue2) Print the pairs End
范例程式码
#include<iostream>
using namespace std;
int main() {
pair <char,int> value('a',7);
pair <string,double> fruit ("grapes",2.30);
pair <string,double> food ("pulao",200);
cout<<"The value of "<<value.first<<" is "<<value.second <<endl;
cout<<"The price of "<<fruit.first<<" is Rs. "<<fruit.second <<endl;
cout<<"The price of "<<food.first<<" is Rs. "<<food.second <<endl;
return 0;
}输出结果
The value of a is 7 The price of grapes is Rs. 2.3 The price of pulao is Rs. 200
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短