0
点赞
收藏
分享

微信扫一扫

c++---map/multimap容器

龙毓七七 2022-04-22 阅读 120
c++

1.map基本概念

简介:

  • map中所有元素都是pair
  • pair中的第一个元素为key(键值),起到索引作用,第二个元素为value(实值)
  • 所有元素都会根据元素的键值自动排序

本质:

  • map/multimap属于关联式容器,底层结构是用二叉树实现

优点:

  • 可以根据key值快速找到value值

map和multimap的区别

  • map不允许容器中有重复key元素
  • multimap允许有重复key元素

2、map构造和赋值

函数原型:

  • map<T1,T2> mp;map默认构造函数
  • map(const map &mp);拷贝构造函数
  • map& operator=(const map &mp);重载等号运算符
#include<map>
using namespace std;


void printmap(map<int, int> &mp)
{
	for (map<int,int>::iterator it = mp.begin(); it != mp.end(); it++)
	{
		cout << (*it).first << " " << it->second << endl;
	}
	cout << endl;
}

void test01()
{
	map<int, int> m;
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(2, 20));
	m.insert(pair<int, int>(3, 25));
	m.insert(pair<int, int>(4, 34));
	m.insert(pair<int, int>(5, 23));
	printmap(m);

	map<int, int> m2(m);
	printmap(m2);
	map<int, int> m3;
	m3 = m2;
	printmap(m3);
}

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

2、map大小和交换

函数原型:

  • size();元素数目
  • empty();是否为空
  • sawp(mp);两个map交换元素
#include<map>
using namespace std;


void printmap(map<int, int> &mp)
{
	for (map<int,int>::iterator it = mp.begin(); it != mp.end(); it++)
	{
		cout << (*it).first << " " << it->second << endl;
	}
	cout << endl;
}

void test01()
{
	map<int, int> m;
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(2, 20));
	m.insert(pair<int, int>(3, 25));
	m.insert(pair<int, int>(4, 34));
	m.insert(pair<int, int>(5, 23));
	printmap(m);

	cout << m.size() << endl;
	map<int, int> m1;
	m1.insert(pair<int, int>(3, 30));
	m1.insert(pair<int, int>(5, 450));
	m1.insert(pair<int, int>(7, 505));
	m1.insert(pair<int, int>(6, 12));
	m1.insert(pair<int, int>(9, 35));
	printmap(m1);

	m.swap(m1);
	printmap(m);
	printmap(m1);

}

int main()
{
	test01();
	
	system("pause");
	return 0;
}
1 10
2 20
3 25
4 34
5 23

5
3 30
5 450
6 12
7 505
9 35

3 30
5 450
6 12
7 505
9 35

1 10
2 20
3 25
4 34
5 23

请按任意键继续. . .

 4、map插入和删除

函数原型:

  • insert(elem);
  • clear();
  • erase(pos); 删除pos迭代器所指的元素,返回下一个元素的迭代器
  • erase(beg,end); 删除区间内所指的元素,返回下一个元素的迭代器
  • erase(key);删除值为key的元素
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cmath>
#include<fstream>
#include<algorithm>//标准算法头文件
#include<deque>
#include<stack>
#include<list>
#include<set>
#include<map>
using namespace std;


void printmap(map<int, int> &mp)
{
	for (map<int,int>::iterator it = mp.begin(); it != mp.end(); it++)
	{
		cout << (*it).first << " " << it->second << endl;
	}
	cout << endl;
}

void test01()
{
	map<int, int> m;
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(2, 20));
	m.insert(pair<int, int>(3, 25));
	m.insert(pair<int, int>(4, 34));
	m.insert(pair<int, int>(5, 23));

	m.insert(make_pair(8, 29));

	m.insert(map<int, int>::value_type(12, 56));

	m[17] = 89;//不建议用来插值,可以用来访问数据
	cout << m[4] << endl;
	printmap(m);

	m.erase(m.begin());
	printmap(m);

	m.erase(3);
	printmap(m);

	m.erase(m.begin(), m.end());
	printmap(m);


}

int main()
{
	test01();
	
	system("pause");
	return 0;
}
34
1 10
2 20
3 25
4 34
5 23
8 29
12 56
17 89

2 20
3 25
4 34
5 23
8 29
12 56
17 89

2 20
4 34
5 23
8 29
12 56
17 89


请按任意键继续. . .

5、map查找与统计

函数原型:

  • find(key);查找key是否存在,存在返回元素迭代器,不存在返回map.end();
  • count(key);统计key的个数
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cmath>
#include<fstream>
#include<algorithm>//标准算法头文件
#include<deque>
#include<stack>
#include<list>
#include<set>
#include<map>
using namespace std;


void printmap(map<int, int> &mp)
{
	for (map<int,int>::iterator it = mp.begin(); it != mp.end(); it++)
	{
		cout << (*it).first << " " << it->second << endl;
	}
	cout << endl;
}

void test01()
{
	map<int, int> m;
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(2, 20));
	m.insert(pair<int, int>(3, 25));
	m.insert(pair<int, int>(4, 34));
	m.insert(pair<int, int>(5, 23));

	m.insert(make_pair(8, 29));

	m.insert(map<int, int>::value_type(12, 56));
	printmap(m);

	map<int, int>::iterator pos = m.find(2);
	if (pos != m.end())
	{
		cout << pos->first << " " << (*pos).second << endl;
	}
	else {
		cout << "没找到" << endl;
	}

	cout << m.count(4) << endl;
}

int main()
{
	test01();
	
	system("pause");
	return 0;
}
1 10
2 20
3 25
4 34
5 23
8 29
12 56

2 20
1
请按任意键继续. . .

5、map排序

#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cmath>
#include<fstream>
#include<algorithm>//标准算法头文件
#include<deque>
#include<stack>
#include<list>
#include<set>
#include<map>
using namespace std;


void printmap(map<int, int> &mp)
{
	for (map<int,int>::iterator it = mp.begin(); it != mp.end(); it++)
	{
		cout << (*it).first << " " << it->second << endl;
	}
	cout << endl;
}

class mycompare
{
public:
	bool operator()(int v1, int v2)
	{
		return v1 > v2;
	}
};

void test01()
{
	map<int, int, mycompare> m;
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(6, 20));
	m.insert(pair<int, int>(30, 25));
	m.insert(pair<int, int>(13, 34));
	m.insert(pair<int, int>(5, 23));
	m.insert(make_pair(8, 29));
	m.insert(map<int, int>::value_type(12, 56));
	for (map<int, int,mycompare>::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << (*it).first << " " << it->second << endl;
	}
	cout << endl;

}

int main()
{
	test01();
	
	system("pause");
	return 0;
}
30 25
13 34
12 56
8 29
6 20
5 23
1 10

请按任意键继续. . .

 案例:

#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cmath>
#include<fstream>
#include<algorithm>//标准算法头文件
#include<deque>
#include<stack>
#include<list>
#include<set>
#include<map>
using namespace std;

#define CHEHUA 0
#define YANFA 1
#define MEISHU 2

class worker
{
public:
	string m_name;
	int m_salsry;

};
void creatworker(vector<worker> &v)
{
	string name = "ABCDEFGHIJ";
	for (int i = 0; i < 10; i++)
	{
		worker worker;
		worker.m_name = "员工";
		worker.m_name += name[i];

		worker.m_salsry = rand() % 10000 + 10000;
		v.push_back(worker);
	}
}
void setgroup(vector<worker> &v, multimap<int, worker> &m)
{
	for (vector<worker>::iterator it = v.begin(); it != v.end(); it++)
	{
		int id = rand() % 3;
		m.insert(make_pair(id, *it));
	}
}
void showworker(multimap<int, worker> &m)
{
	cout << "策划部门" << endl;
	int count = m.count(CHEHUA);
	int index = 0;
	multimap<int, worker>::iterator pos = m.find(CHEHUA);
	for (; pos != m.end() && index < count; pos++, index++)
	{
		cout << "姓名" << pos->second.m_name << " 工资" << pos->second.m_salsry << endl;
	}
	cout << "研发部门" << endl;
	count = m.count(YANFA);
	index = 0;
	pos = m.find(YANFA);
	for (; pos != m.end() && index < count; pos++, index++)
	{
		cout << "姓名" << pos->second.m_name << " 工资" << pos->second.m_salsry << endl;
	}
	cout << "美术部门" << endl;
	count = m.count(MEISHU);
	index = 0;
	pos = m.find(MEISHU);
	for (; pos != m.end() && index < count; pos++, index++)
	{
		cout << "姓名" << pos->second.m_name << " 工资" << pos->second.m_salsry << endl;
	}
}

int main()
{
	srand((unsigned int)time(NULL));
	vector<worker> vworker;
	creatworker(vworker);

	multimap<int, worker> mworker;
	setgroup(vworker, mworker);
	showworker(mworker);
	/*for (vector<worker>::iterator it = vworker.begin(); it != vworker.end(); it++)
	{
		cout << "姓名:" << it->m_name << " 工资:" << it->m_salsry << endl;
	}*/
	
	system("pause");
	return 0;
}
策划部门
姓名员工F 工资16954
姓名员工H 工资16942
研发部门
姓名员工A 工资12572
姓名员工B 工资13740
姓名员工G 工资19223
姓名员工I 工资12946
美术部门
姓名员工C 工资12303
姓名员工D 工资14672
姓名员工E 工资12096
姓名员工J 工资11162
请按任意键继续. . .
举报

相关推荐

0 条评论