标签:style blog color os io for div amp
11.12 编写程序,读入string和int的序列,将每个string和int存入一个pair中,pair保存在一个vector中。
#include<iostream> #include<vector> #include<utility> #include<string> using namespace std; int main() { vector<pair<string,int>> vec; string str; int n; while(cin>>str&&cin>>n) { vec.push_back(make_pair(str,n)); } cout<<"输出如下:"<<endl; for(auto r:vec) cout<<r.first<<" "<<r.second<<endl; return 0; }
11.13 用三种方式实现构造pair
#include<iostream> #include<vector> #include<utility> #include<string> using namespace std; int main() { vector<pair<string,int>> vec; string str; int n; while(cin>>str&&cin>>n) { vec.push_back(make_pair(str,n)); vec.push_back(pair<string,int>(str,n)); vec.push_back(pair<string,int>{str,n}); } cout<<"输出如下:"<<endl; for(auto r:vec) cout<<r.first<<" "<<r.second<<endl; return 0; }
11.14
#include<iostream> #include<map> #include<string> #include<vector> #include<utility> using namespace std; int main() { vector<pair<string,string>> student; map<string,vector<pair<string,string>>> family; string firstname; string lastname; string birthday; /*while(cin>>lastname&&lastname!="0") { family.insert(make_pair(lastname,student)); }*/ while(cin>>lastname) { while(cin>>firstname&&cin>>birthday&&firstname!="q") family[lastname].push_back(make_pair(firstname,birthday)); } for(auto s:family) { cout<<s.first<<" firstname "; for(auto r:s.second) cout<<r.first<<" "<<r.second<<" "; cout<<endl; } return 0; }
标签:style blog color os io for div amp
原文地址:http://www.cnblogs.com/wuchanming/p/3920436.html