map的初始化、erase()、写入与写出

运行代码:

//map的初始化、erase()、写入与写出
#include"std_lib_facilities.h"

istream& operator>>(istream& is, map&mm)
{
	string ss="";
	int ii=0;
	is >> ss;
	if(is>>ii)mm[ss] = ii;
    return is;
}

ostream& operator<<(ostream& os,  map& mm)
{
	for (map::iterator it = mm.begin(); it != mm.end(); it++)
	{
		os << "( " << it->first << " , " << it->second << " )" << "\n";
	}
	return os;
}

int main()
try
{
	mapmsi;

	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::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;
}

运行结果:

map的初始化、erase()、写入与写出_第1张图片

你可能感兴趣的:(C++,map,算法)