Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

โปรแกรม C++ เพื่อใช้คู่ใน STL


คู่คือคอนเทนเนอร์อย่างง่ายซึ่งประกอบด้วยออบเจ็กต์ข้อมูลสองรายการ:

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

สามารถกำหนดคู่เปรียบเทียบและคัดลอกได้ ใช้เพื่อรวมค่า 2 ค่าเข้าด้วยกันซึ่งอาจแตกต่างกันในประเภท

ไวยากรณ์คือ :คู่ชื่อตัวแปร(datavalue1, datavalue2).

อัลกอริทึม

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