●map基本概念
map中的所有元素都是pair,pair中第一个元素为key(键值),第二个元素为value(实值),并且所有元素会根据元素的键值自动进行从小到大的排序。它可以根据key的值快速的找到value的值。map与multimap为关联式容器,map不允许容器中有重复的key值,而multimap允许容器中有重复的key值,两者底层的原理是用二叉树实现的。
●map构造和赋值
函数原型:
1.构造
■map<T1, T2> mp //map默认构造函数
■map(const map &mp) //拷贝构造函数
2.赋值
■map& operator=(const map &mp) //重载等号操作符
#include<iostream> #include<map> using namespace std; void printmap(map<int,int>&m) { for (map<int, int>::iterator i = m.begin(); i != m.end(); i++) { cout << "key=" << i->first << " " << "value=" << i->second << endl; } cout << endl; } void text() { map<int, int>m; for (int i = 1, j = 10; i <= 10; i++, j += 10) { m.insert(pair<int,int>(i,j)); } //拷贝构造 map<int, int>m1(m); printmap(m1); //赋值 map<int, int>m2; m2 = m1; printmap(m2); } int main() { text(); }
●map大小和交换
函数原型:
1.大小
■size() //返回容器中元素的数目
■empty() //判断容器是否为空
2.交换
■swap(st) //交换两个集合容器
#include<iostream> #include<map> using namespace std; void swap(map<int, int>& m1, map<int, int>& m2) { m1.swap(m2); } void issize(map<int, int>& m) { cout << "map的大小为:" << m.size() << endl; } void isempty(map<int,int>&m) { if (m.empty()) { cout << "map为空" << endl; } else { cout << "map不为空" << endl; issize(m); } } void printmap(map<int,int>&m) { for (map<int, int>::iterator i = m.begin(); i != m.end(); i++) { cout <<"value=" << i->second << endl; } cout << endl; } void text() { map<int, int>m; m.insert(pair<int, int>(1, 1)); m.insert(pair<int, int>(2, 3)); m.insert(pair<int, int>(3, 5)); m.insert(pair<int, int>(4, 7)); m.insert(pair<int, int>(5, 9)); //判断是否为空,大小为多少 isempty(m); //交换 map<int, int>m1; m1.insert(pair<int, int>(1, 2)); m1.insert(pair<int, int>(2, 4)); m1.insert(pair<int, int>(3, 6)); m1.insert(pair<int, int>(4, 8)); m1.insert(pair<int, int>(5, 10)); cout << "交换前:" << endl; printmap(m); printmap(m1); cout << "交换后:" << endl; swap(m,m1); printmap(m); printmap(m1); } int main() { text(); }
●map插入和删除
函数原型:
1.插入
■insert(elem); //在容器中插入元素
2.删除
■clear() //清除所有元素
■erase(pos) //删除pos迭代器所指的元素,返回下一个元素的迭代器
■erase(beg,end) //删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器
■erase(key) //删除容器中值为key的元素
#include<iostream> #include<map> using namespace std; void printmap(map<int,int>&m) { for (map<int, int>::iterator i = m.begin(); i != m.end(); i++) { cout << "key=" << i->first << " " << "value=" << i->second << endl; } cout << endl; } void text() { map<int, int>m; //插入 for (int i = 1, j = 1; i <= 5; i++, j += 1) { m.insert(pair<int,int>(i,j)); } for (int i = 6, j = 7; i <= 10; i++, j += 1) { m.insert(make_pair(i, j)); } for (int i = 11, j = 10; i <= 15; i++, j += 1) { m[i] = j; } printmap(m); //删除 m.erase(m.begin()); m.erase(2); m.erase(3); m.erase(13); m.erase(14); printmap(m); //清空 //m.erase(m.begin(), m.end()); m.clear(); printmap(m); } int main() { text(); }
●map查找和统计
函数原型:
1.查找
■find(key) //查找key是否存在,若存在,返回该键 的元素的迭代器;若不存在,返回set.end()
2.统计
■count(key) //统计key的元素个数
#include<iostream> #include<map> using namespace std; void count(map<int, int>& m) { cout << "请统计指定key下的元素个数:"<<endl; int n; cin >> n; cout << "num=" << m.count(n) << endl; //map不允许插入重复key元素,所以统计结果要么1,要么0 } void find(map<int,int>&m) { cout << "请输入要查找的key:" << endl; int n; cin >> n; map<int,int>::iterator i=m.find(n); if (i!=m.end()) { cout << "该map容器中的元素:" << i->second << endl; } else { cout << "未找到元素" << endl; } } void printmap(map<int,int>&m) { for (map<int, int>::iterator i = m.begin(); i != m.end(); i++) { cout << "key=" << i->first << " " << "value=" << i->second << endl; } cout << endl; } void text() { map<int, int>m; for (int i = 1, j = 10; i <= 10; i++, j += 10) { m.insert(make_pair(i,j)); } m.insert(make_pair(10,100)); printmap(m); //查找 find(m); //统计 count(m); } int main() { text(); }
●map排序(map初始排序顺序为从小到大,用仿函数将其改为从大到小)
1.内置数据类型排序
#include<iostream> #include<map> using namespace std; class compare { public: bool operator()(int m1,int m2) const { return m1 > m2; } }; void printmap(map<int, int, compare>&m) { for (map<int, int, compare>::iterator i = m.begin(); i != m.end(); i++) { cout << "key=" << i->first << " " << "value=" << i->second << endl; } } void text() { map<int, int,compare>m; for (int i = 1, j = 10; i <= 10; i++, j += 10) { m.insert(make_pair(i,j)); } printmap(m); } int main() { text(); }
2.自定义数据类型排序
#include<iostream> #include<map> #include<string> using namespace std; class person { public: string name; int age; person(string name, int age) { this->name = name; this->age = age; } }; class compare { public: bool operator()(int m1,int m2)const { return m1 > m2; } }; void printmap(map<int, person, compare>& m) { for (map<int, person, compare>::iterator i = m.begin(); i != m.end(); i++) { cout << "key=" << i->first << " " <<"姓名:"<<i->second.name << "年龄:" << i->second.age << endl; } } void text() { person p1("张三", 19); person p2("李四", 20); person p3("王五", 21); map<int, person, compare>m; m.insert(make_pair(1, p1)); m.insert(make_pair(2, p2)); m.insert(make_pair(3, p3)); printmap(m); } int main() { text(); }