//10.2.cpp
//至少使用三種方法創建pair對象。
//編寫三個版本的程序,分別采用不同的方法來創建pair對象
//方法1:在定義pair對象時提供初始化式來創建pair對象#include<iostream>
#include<vector>
#include<utility>
#include<string>
using namespace std;int main()
{pair<string,int> sipr;string str;int ival;vector<pair<string,int>> pvec;cout<<"Enter a string and a integer(Ctrl-z to end):"<<endl;while(cin>>str>>ival){sipr=pair<string,int>(str,ival);pvec.push_back(sipr);}return 0;
}//方法2:直接訪問pair對象來創建pair對象#include<iostream>
#include<vector>
#include<utility>
#include<string>
using namespace std;int main()
{pair<string,int> sipr;string str;int ival;vector<pair<string,int>> pvec;cout<<"Enter a string and a integer(Ctrl-z to end):"<<endl;while(cin>>str>>ival){sipr.first=str;sipr.second=ival;pvec.push_back(sipr);}return 0;
}//方法3:用make_pair函數來生成pair對象
#include<iostream>
#include<vector>
#include<utility>
#include<string>
using namespace std;int main()
{pair<string,int> sipr;string str;int ival;vector<pair<string,int>> pvec;cout<<"Enter a string and a integer(Ctrl-z to end):"<<endl;while(cin>>str>>ival){sipr=make_pair(str,ival);pvec.push_back(sipr);}return 0;
}
轉載于:https://www.cnblogs.com/wwj9413/archive/2012/02/19/2638650.html