map< int, int>mymap;
//方法1
mymap.insert(pair<int, int>(10, 10));
//方法2
mymap.insert(make_pair(20, 20));
//方法3
mymap.insert(map<int, int>::value_type(30, 30));
//方法4
mymap[40] = 40;
for (map<int, int>::iterator it = mymap.begin(); it != mymap.end(); it++)
{
cout << "key is: " << it->first << ";"<< " value is :" << it->second << endl;
}
NOTE:
实例: 在1 2 3 4 中equal_range(2) ,第一个>=2 的是2,第一个> 2是3,应该返回 2 , 3
map<int, int> mymap;
mymap.insert(make_pair(1,10));
mymap.insert(make_pair(2,20));
mymap.insert(make_pair(3,30));
mymap.insert(make_pair(4, 40));
pair<map<int, int>::iterator, map<int, int>::iterator> mypair = mymap.equal_range(2);
cout << mypair.first->first << " : "<< mypair.first->second << endl;
cout << mypair.second->first<< " : " << mypair.second->second << endl;
#include "stdafx.h"
#include
#include
using namespace std;
class Key
{
public:
Key(int ID, int age):m_ID(ID),m_age(age) {}
int m_ID;
int m_age;
};
struct Compare //自定义的规则,用Key.m_age排序 ,从小到大
{
bool operator()(const Key& key1, const Key& key2)
{
return key1.m_age < key2.m_age;
}
};
int main()
{
map<Key, int, Compare> mymap;
mymap.insert(make_pair(Key(1, 1), 1));
mymap.insert(make_pair(Key(2, 2), 2));
mymap.insert(make_pair(Key(3, 3), 3));
mymap.insert(make_pair(Key(4, 4), 4));
for (map<Key, int, Compare>::iterator it=mymap.begin(); it != mymap.end(); it++)
{
cout << it->first.m_age << " : " << it->first.m_ID << " : " << it->second << endl;
}
return 0;
}
结果:struct Compare 返回的是 return key1.m_age > key2.m_age;
#include "stdafx.h"
#include
#include
#include
#include
#include
using namespace std;
#define SALE_DEPARTMENT 1
#define DEVELOP_DEPARTMENT 2
#define FINACIAL_DEPARTMENT 3
class Worker
{
public:
string m_name;
string m_tele;
int m_age;
int m_salary;
};
//创建员工
void Creat_Worker(vector<Worker>& Workers)
{
string seed = "ABCDE";
for (int i = 0; i < 5; i++)
{
Worker Wok;
Wok.m_name = "员工";
Wok.m_name+=seed[i];
Wok.m_age = rand() % 10 + 20;
Wok.m_salary = rand() % 10000 + 10000;
Wok.m_tele = "010-73829783";
Workers.push_back(Wok);
}
}
//员工分组
void Worker_Group(vector<Worker>& Workers, multimap <int, Worker>& Workergroup)
{
srand(time(NULL));
for (vector<Worker>::iterator it = Workers.begin(); it != Workers.end(); it++)
{
int DeparID = rand() % 3 + 1;
switch (DeparID)
{
case SALE_DEPARTMENT:
Workergroup.insert(make_pair(SALE_DEPARTMENT, (*it)));
break;
case DEVELOP_DEPARTMENT:
Workergroup.insert(make_pair(DEVELOP_DEPARTMENT, (*it)));
break;
case FINACIAL_DEPARTMENT:
Workergroup.insert(make_pair(FINACIAL_DEPARTMENT, (*it)));
break;
default:
break;
}
}
}
//打印部门员工
void ShowWorkers(multimap <int, Worker>& Workergroup, int dep)
{
//打印销售部SALE_DEPARTMENT
multimap<int, Worker>::iterator it = Workergroup.find(dep);
int num = Workergroup.count(dep);
for (it; it != Workergroup.end() && num != 0; it++, num--)
{
cout << "name: " << it->second.m_name << endl;
cout << "age:" << it->second.m_age << endl;
cout << "salary:" << it->second.m_salary << endl;
cout << "tel:" << it->second.m_tele << endl;
}
}
//打印每个部门的员工信息
void Print_Worker(multimap <int, Worker>& Workergroup)
{
cout << "---------------销售部---------------" << endl;
ShowWorkers(Workergroup, SALE_DEPARTMENT);
cout << "---------------研发部---------------" << endl;
ShowWorkers(Workergroup, DEVELOP_DEPARTMENT);
cout << "---------------财务部---------------" << endl;
ShowWorkers(Workergroup, FINACIAL_DEPARTMENT);
}
int main()
{
vector<Worker> Workers;
multimap <int, Worker> Workergroup;
//创建员工
Creat_Worker(Workers);
//员工分组
Worker_Group(Workers, Workergroup);
//打印每个部门的员工信息
Print_Worker(Workergroup);
return 0;
}