运行代码:
//map的初始化、erase()、写入与写出
#include"std_lib_facilities.h"
istream& operator>>(istream& is, map<string, int>&mm)
{
string ss="";
int ii=0;
is >> ss;
if(is>>ii)mm[ss] = ii;
return is;
}
ostream& operator<<(ostream& os, map<string, int>& mm)
{
for (map<string, int>::iterator it = mm.begin(); it != mm.end(); it++)
{
os << "( " << it->first << " , " << it->second << " )" << "\n";
}
return os;
}
int main()
try
{
map<string, int>msi;
while (cin >> msi);
msi["lecture"] = 21;
msi["Max"] = 23;
msi["Jane"] = 24;
msi["Tom"] = 33;
msi["Sun"] = 55;
msi["lecccture"] = 213;
msi["Maddx"] = 233;
msi["Janeee"] = 4;
msi["Tomd"] = 343;
msi["Sude"] = 25;
map<string, int>::iterator p = msi.find("Sun");
msi.erase(p);
cout << msi;
return 0;
}
catch (exception& e) {
cerr << "error:" << e.what() << '\n';
keep_window_open();
return 1;
}
catch (...) {
cerr << "Oops:unknown exception!\n";
keep_window_open();
return 2;
}
运行结果: