map映射

#include <iostream>
#include <map>
#include <string>

using namespace std;

int main()
{
	map<int, string> a; // map是映射,
	multimap<int, string> ma;  // multimap是多映射,

	//                                 key键  value值
	a.insert(map<int, string>::value_type(1, "one"));
	a.insert(map<int, string>::value_type(2, "two"));
	a.insert(map<int, string>::value_type(3, "three"));
	a.insert(make_pair(-1, "minus one"));
	a.insert(pair<int, string>(1000, "one thousand"));
	a[1000000] = "one million";

	cout << "最简单的查找。" << endl;
	cout << a[3] << endl; // 输出的是three,
	

	cout << "map里一共有" << a.size() << "个Key-Value对数据" << endl;
	cout << "这些数据是:" << endl;

	map<int, string>::const_iterator i;  // 循环根据大小来循环的,
	for (i = a.begin(); i != a.end(); ++i)
	{
		cout << "Key:" << i->first << " ";
		cout << "Value:" << i->second.c_str();
		cout << endl;
	}

	if (a.erase(-1) > 0)
		cout << endl << "删除-1成功!" << endl;

	map<int, string>::iterator iElementFound = a.find(1000);
	if (iElementFound != a.end())
	{
		a.erase(iElementFound);
		cout << "输出1000成功," << endl;
	}

	a.erase(a.lower_bound(1), a.upper_bound(1));  // 删除所有的1,从第一个到最后一个,

	/*ma.insert(multimap<int, string>::value_type(3, "three"));
	ma.insert(multimap<int, string>::value_type(45, "Forty five")); // multimap允许找重复的数据,
	ma.insert(make_pair(-1, "minus one"));
	ma.insert(pair<int, string>(1000, "one thousand"));
	ma.insert(pair<int, string>(1000, "one thousand"));

	cout << endl << "multimap里有" << ma.size() << "个数据。" << endl;

	multimap<int, string>::const_iterator im;
	for (im = ma.begin(); im != ma.end(); ++im);
	{
		cout << "Key: " << im->first;
		cout << " Value: " << im->second;
		cout << endl;
	}

	cout << "multimap里有" << ma.count(1000) << "个1000!" << endl;*/

    
	return 0;
}

你可能感兴趣的:(map映射)