Map的操作

map 存放元素和遍歷
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <map>
#include <string>
using namespace std;


void map_operate()
{
	map<int, string> map1;

	// 方法1
	map1.insert(pair<int, string>(1, "teacher01"));
	map1.insert(pair<int, string>(2, "teacher02"));

	// 方法2
	map1.insert(make_pair(3, "teacher03"));
	map1.insert(make_pair(4, "teacher04"));

	// 方法3
	map1.insert(map<int, string>::value_type(5, "teacher05"));
	map1.insert(map<int, string>::value_type(6, "teacher06"));

	// 方法4
	map1[7] = "teacher07";
	map1[8] = "teacher08";

	// 容器遍歷
	for (map<int, string>::iterator it = map1.begin(); it != map1.end(); it++)
	{
		cout << it->first << "\t" << it->second << endl;
	}
	cout << "遍歷結束" << endl;


}

int main()
{
	map_operate();
	system("pause");
	return 0;
}

上述四種插入方法的異同:

前三種方法返回的是pair<iterator, bool>

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <map>
#include <string>
using namespace std;


void map_operate()
{
	map<int, string> map1;

	// 方法1
	pair<map<int, string>::iterator, bool> mypair1 = map1.insert(pair<int, string>(1, "teacher01"));
	map1.insert(pair<int, string>(2, "teacher02"));

	// 方法2
	pair<map<int, string>::iterator, bool> mypair3 = map1.insert(make_pair(3, "teacher03"));
	map1.insert(make_pair(4, "teacher04"));

	// 方法3
	pair<map<int, string>::iterator, bool> mypair5 = map1.insert(map<int, string>::value_type(5, "teacher05"));
	if (mypair5.second != true)
	{
		cout << "key 5 插入失敗" << endl;
	}
	else
	{
		cout << mypair5.first->first << " : " << mypair5.first->second<< endl;
	}
	// 插入2個5  會插入失敗
	pair<map<int, string>::iterator, bool> mypair6 = map1.insert(map<int, string>::value_type(5, "teacher06"));
	if (mypair6.second != true)
	{
		cout << "key 5 插入失敗" << endl;
	}
	else
	{
		cout << mypair6.first->first << " : " << mypair6.first->second << endl;
	}


	// 方法4   相同的 key 會覆蓋
	map1[7] = "teacher07";
	map1[7] = "teacher77";

	// 容器遍歷
	for (map<int, string>::iterator it = map1.begin(); it != map1.end(); it++)
	{
		cout << it->first << "\t" << it->second << endl;
	}
	cout << "遍歷結束" << endl;


}

int main()
{
	map_operate();
	system("pause");
	return 0;
}

前三種方法返回的是pair<iterator, bool> 若 key存在,那麼會報錯 方法四若key已經存在,則會修改(覆蓋)

multimap 的使用案例
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <map>
#include <string>
using namespace std;

/*
* 一個key可以對應多個value  =  分組
* 通過multimap進行信息的插入/保存/顯示
* 分部門顯示員工信息
*/

class Person
{
public:
	string	name;
	int		age;
	string	tel;
	double	sal;


};


void multimapDemo()
{
	Person p1, p2, p3, p4, p5;
	p1.name = "王1";
	p1.age = 31;

	p2.name = "王2";
	p2.age = 32;

	p3.name = "張3";
	p3.age = 33;

	p4.name = "張4";
	p4.age = 34;

	p5.name = "趙5";
	p5.age = 35;

	multimap<string, Person> map1;

	// sale 部門
	map1.insert(make_pair("sale", p1));
	map1.insert(make_pair("sale", p2));
	// development
	map1.insert(make_pair("development", p3));
	map1.insert(make_pair("development", p4));
	// Financial
	map1.insert(make_pair("financial", p5));

	for (multimap<string, Person>::iterator it = map1.begin(); it != map1.end(); it++)
	{
		cout << it->first << "\t" << it->second.name << endl;
	}
	cout << "遍歷結束" << endl;

	// 按部門顯示員工信息
	int count = map1.count("development");
	cout << "development 部門人數 : " << count << endl;

	cout << "development 部門員工信息 : " << endl;
	multimap<string, Person>::iterator it2 = map1.find("development");

	int tag = 0;
	while (it2 != map1.end() && tag < count)
	{
		cout << it2->first << "\t" << it2->second.name << endl;
		it2++;
		tag++;
	}
}


// 修改容器的內容   把 age = 32 的name 修改成爲name32
void multimapDemo_02()
{
	Person p1, p2, p3, p4, p5;
	p1.name = "王1";
	p1.age = 31;

	p2.name = "王2";
	p2.age = 32;

	p3.name = "張3";
	p3.age = 33;

	p4.name = "張4";
	p4.age = 34;

	p5.name = "趙5";
	p5.age = 35;

	multimap<string, Person> map1;

	// sale 部門
	map1.insert(make_pair("sale", p1));
	map1.insert(make_pair("sale", p2));
	// development
	map1.insert(make_pair("development", p3));
	map1.insert(make_pair("development", p4));
	// Financial
	map1.insert(make_pair("financial", p5));

	cout << "按照條件檢索數據進行修改 : " << endl;
	for (multimap<string, Person>::iterator it = map1.begin(); it != map1.end(); it++)
	{
		if (it->second.age == 32)
		{
			it->second.name = "name32";
		}
		cout << it->first << "\t" << it->second.name << endl;
	}
	cout << "遍歷結束" << endl;
}



int main()
{
	// multimapDemo();
	multimapDemo_02();

	system("pause");
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章