当前位置:编程学习 > C/C++ >>

[STL基础]utility工具文件——pair组对单位模板类

[cpp] 
 //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());  
    }   
}  
 
补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,