C++程序实现STL中的对


pair是一个简单的容器,它由两个数据对象组成

‘first’ = The first element is referenced as ‘first’
‘second’ = the second element and the order is fixed (first, second).

pair可以被赋值、比较和复制。它常用于把可能类型不同的两个值组合在一起。

语法是: 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

更新于: 2019-07-30

287次浏览

开启您的职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.