C++ Primer(第5版) 练习 11.14
练习 11.14 扩展你在11.2.1节练习(第378页)中编写的孩子姓到名的map,添加一个pair的vector,保存孩子的名和生日。
环境:Linux Ubuntu(云服务器)
工具:vim
代码块
# include <iostream>
# include <iomanip>
# include <vector>
# include <map>
# include <set>
# include <utility>
# include <algorithm>
using namespace std; int main ( ) { map< string, vector< pair< string, string>> > home; string lastName, firstName, birth; cout<< "Enter Name, birthday:" << endl; cin>> firstName>> lastName>> birth; home[ lastName] . push_back ( make_pair ( firstName, birth) ) ; while ( cin>> firstName>> lastName>> birth) { if ( home. find ( lastName) != home. end ( ) ) { home[ lastName] . push_back ( make_pair ( firstName, birth) ) ; } else { home[ lastName] . push_back ( make_pair ( firstName, birth) ) ; } } cout<< endl; cout<< "Name List:" << endl; for ( const auto h : home) { for ( const auto b : h. second) { cout<< setw ( 8 ) << right<< b. first<< " " << setw ( 8 ) << left<< h. first<< " " << b. second<< endl; } } return 0 ;
}
运行结果显示如下