map中所有元素都是pair
所有元素会根据元素中的key值自动排序
map和multimap区别:
1.map不允许容器中有重复key值元素
2.multimap允许容器中有重复key值元素
map构造和赋值
#include <iostream>
#include<map>
using namespace std;
void printmap(map<int,int>&m)
{
for (map<int,int>::iterator it=m.begin();it!=m.end();it++)
{
cout << (*it).first <<(*it).second<< endl;//first是key值,second是value值
}
}
//map容器 构造和赋值
void test1()
{
//自定义数据类型应指定排序规则
map<int, int>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(4, 40));
printmap(m);
map<int, int>m2(m);
map<int, int>m3;
m3 = m2;
}
map大小和交换
#include <iostream>
#include<map>
using namespace std;
void printmap(map<int,int>&m)
{
for (map<int,int>::iterator it=m.begin();it!=m.end();it++)
{
cout << (*it).first <<(*it).second<< endl;//first是key值,second是value值
}
}
//map容器 构造和赋值
void test1()
{
//自定义数据类型应指定排序规则
map<int, int>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(4, 40));
printmap(m);
if (m.empty())
{
cout << "为空" << endl;
}
else
{
cout << "不为空" << endl;
cout << m.size() << endl;
}
map<int, int>m1;
m1.insert(pair<int, int>(1, 10));
m1.insert(pair<int, int>(6, 60));
m1.insert(pair<int, int>(5, 50));
m1.insert(pair<int, int>(4, 40));
printmap(m);
m.swap(m1);
printmap(m);
printmap(m1);
}
#include <iostream>
#include<map>
using namespace std;
void printmap(map<int,int>&m)
{
for (map<int,int>::iterator it=m.begin();it!=m.end();it++)
{
cout << (*it).first <<(*it).second<< endl;//first是key值,second是value值
}
}
//map容器 插入和删除
void test1()
{
//自定义数据类型应指定排序规则
map<int, int>m;
//插入第一种
m.insert(pair<int, int>(1, 10));
//第二种
m.insert(make_pair(2, 20));
//第三种
m.insert(map<int, int>::value_type(3, 30));
//第四种
m[4] = 40;
printmap(m);
//删除 只会按key进行删除
m.erase(m.begin());
m.erase(3);
m.erase(m.begin(), m.end());
m.clear();
}
map容器 查找和统计
#include <iostream>
#include<map>
using namespace std;
void printmap(map<int,int>&m)
{
for (map<int,int>::iterator it=m.begin();it!=m.end();it++)
{
cout << (*it).first <<(*it).second<< endl;//first是key值,second是value值
}
}
//map容器 查找和统计
void test1()
{
//自定义数据类型应指定排序规则
map<int, int>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(4, 40));
//查找
//只会按照key值查找,返回的是迭代器,如果没找到返回end()
map<int, int>::iterator pos= m.find(3);
if (pos != m.end())
{
cout <<"查找到了"<< (*pos).first << (*pos).second << endl;
}
else
{
cout << "没找到" << endl;
}
//统计
//map的统计结果只有0和1,multimap可以有其他值
int num = m.count(2);
cout << num << endl;
}
map容器的排序
#include <iostream>
#include<map>
using namespace std;
void printmap(map<int,int>&m)
{
for (map<int,int>::iterator it=m.begin();it!=m.end();it++)
{
cout << (*it).first <<(*it).second<< endl;//first是key值,second是value值
}
}
//改变排序规则为降序
class mycompare
{
public:
bool operator()(int v1, int v2)
{
return v1 > v2;
}
};
//map容器 排序
void test1()
{
map<int, int,mycompare>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(5, 20));
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(2, 40));
}
员工分组案例
#include <iostream>
#include<string>
#include<vector>
#include<map>
using namespace std;
class worker
{
public:
string m_name;
int m_salary;
};
void creatworker(vector<worker>& v)
{
string nameseed = "ABCDEFGHIJ";
for (int i = 0; i < 10; i++)
{
worker work;
work.m_name = "员工";
work.m_name += nameseed[i];
work.m_salary = rand() % 10000 + 10000;
v.push_back(work);
}
}
void setgroup(vector<worker>& v, multimap<int, worker>&mworker)
{
for (vector<worker>::iterator it = v.begin(); it != v.end(); it++)
{
//产生随机部门编号
int deptID = rand() % 3;
//将员工插入分组
mworker.insert(make_pair(deptID, * it));
}
}
void showworkerbygroup(multimap<int, worker>&mworker)
{
for (multimap<int, worker>::iterator it = mworker.begin(); it != mworker.end(); it++)
{
if ((*it).first == 0)
{
cout << "姓名:" << (*it).second.m_name << "部门:策划" << "工资:" << (*it).second.m_salary << endl;
}
else if ((*it).first == 1)
{
cout << "姓名:" << (*it).second.m_name << "部门:美术" << "工资:" << (*it).second.m_salary << endl;
}
else if ((*it).first == 2)
{
cout << "姓名:" << (*it).second.m_name << "部门:技术" << "工资:" << (*it).second.m_salary << endl;
}
}
}
int main(){
//创建员工
vector<worker>v;
creatworker(v);
for (vector<worker>::iterator it = v.begin(); it != v.end(); it++)
{
cout << (*it).m_name << " " << (*it).m_salary << endl;
}
//员工分组
multimap<int, worker>mworker;
setgroup(v,mworker);
showworkerbygroup(mworker);
system("pause");
return 0;
}