Loading... ## 基本概念: ### 简介: map中所有元素都是pair pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值) 所有元素都会根据元素的键值自动排序 ### 本质: map/multimap属于关联式容器,底层结构是用二叉树 ### 优点: 可以根据key值快速找到value ### map和multimap区别: map不允许容器中有重复key值元素 multimap允许容器中有重复key值元素 ## map构造和赋值: ### 功能描述: 对map容器进行构造和赋值操作 ### 构造: map <T1, T2>mp;//map默认构造函数 map(const map &mp);//拷贝构造函数 ### 赋值: map& operator = (conat map &mp);//重载等号操作符 ### 示例: ```cpp #include<iostream> #include<map> using namespace std; void print_map(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 text() { map<int, int>mp;//默认构造 mp.insert(pair<int, int>(1, 3)); mp.insert(pair<int, int>(2, 6)); mp.insert(pair<int, int>(5, 15)); mp.insert(pair<int, int>(3, 9)); mp.insert(pair<int, int>(4, 12)); print_map(mp); map<int, int>mp1(mp);//拷贝构造 print_map(mp); map<int, int>mp2;//赋值构造 mp2 = mp; print_map(mp); } int main()//map中所有元素都是成对出现,插入数据时候要使用对组(pair) { text(); return 0; } ``` ## map大小和交换: ### 功能描述: 统计map容器大小以及交换map容器 ### 函数原型: size();//返回容器中元素的数目 empty();//判断容器是否为空 swap(st);//交换两个集合容器 示例: ```cpp #include<iostream> #include<map> using namespace std; void print_map(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 text() { map<int, int>mp;//默认构造 mp.insert(pair<int, int>(1, 3)); mp.insert(pair<int, int>(2, 6)); mp.insert(pair<int, int>(5, 15)); mp.insert(pair<int, int>(3, 9)); mp.insert(pair<int, int>(4, 12)); print_map(mp); if (mp.empty()) { cout << "容器为空!" << endl; } else { cout << "容器不为空!容器大小为:" << mp.size() << endl; } } void swap_map() { map<int, int>mp;//默认构造 mp.insert(pair<int, int>(1, 3)); mp.insert(pair<int, int>(2, 6)); mp.insert(pair<int, int>(5, 15)); mp.insert(pair<int, int>(3, 9)); mp.insert(pair<int, int>(4, 12)); map<int, int>mp1;//默认构造 mp1.insert(pair<int, int>(1, 8)); mp1.insert(pair<int, int>(2, 9)); mp1.insert(pair<int, int>(5, 5)); mp1.insert(pair<int, int>(3, 9)); mp1.insert(pair<int, int>(4, 2)); cout << "交换前:" << endl; print_map(mp); print_map(mp1); cout << "交换后:" << endl; mp.swap(mp1); print_map(mp); print_map(mp1); } int main() { text(); swap_map(); return 0; } ``` ## map插入和删除: ### 功能描述: map容器进行插入数据和删除数据 ### 函数原型: insert(elem);//在容器中插入元素 clear();//清除所以元素 erase(pos);//删除pos迭代器所指的元素,返回下一个元素的迭代器 erase(beg, end);//删除区间[beg, end]的所以元素,返回下一个元素的迭代器 erase(key);//删除容器中值为key的元素 示例: ```cpp #include<iostream> #include<map> using namespace std; void print_map(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 text() { map<int, int>mp;//默认构造 mp.insert(pair<int, int>(1, 3)); //第一种插入方法 mp.insert(make_pair(2, 4));//第二种插入方法 mp.insert(map<int, int>::value_type(3, 6));//第三种插入方法 mp[4] = 5;//第四中插入方法,但是不建议使用,原因是如果不赋值的话系统自动默认插入为0 print_map(mp); mp.erase(mp.begin());//删除首项元素 print_map(mp); mp.erase(3);//a删除键为3的键值对 print_map(mp); mp.erase(mp.begin(), mp.end());//清空容器 mp.clear(); print_map(mp); } int main() { text(); return 0; } ``` ## map查找和统计 ### 功能描述: 对map容器进行查找数据以及统计数据 ### 函数原型: find(key);//查找key是否存在,返回该键的元素的迭代器;若不存在,返回set.end(); count(key);//统计key的元素个数 ### 示例: ```cpp #include<iostream> #include<map> using namespace std; void print_map(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 text() { map<int, int>mp;//默认构造 mp.insert(make_pair(1, 8)); mp.insert(make_pair(4, 9)); mp.insert(make_pair(3, 4)); mp.insert(make_pair(6, 2)); map<int, int>::iterator it = mp.find(3); if (it != mp.end()) { cout << "找到键为:" << it->first << " 值为:" << it->second << endl; } else { cout << "没有找到该键!" << endl; } int sum = mp.count(3);//sum只能是0或者1,当sum为0时没找到键,当sum为找到键(只能为1是因为重复的键值插不进容器中,multimap除外) cout << sum << endl; } int main() { text(); return 0; } ``` ## map容器排序 ### 主要技术点: 利用仿函数,可以改变排序规则 ### 示例: ```cpp #include<iostream> #include<map> using namespace std; class Person//默认从小到大,利用仿函数从大到小 { public: bool operator()(int p1, int p2)const { return p1 > p2; } }; void print_map(map<int, int, Person>& mp) { for (map<int, int, Person>::iterator it = mp.begin(); it != mp.end(); it++) { cout << "键为:" << it->first << " 值为:" << it->second <<endl; } cout << endl; } void text() { map<int, int, Person>mp;//默认构造 mp.insert(make_pair(1, 8)); mp.insert(make_pair(4, 9)); mp.insert(make_pair(3, 4)); mp.insert(make_pair(6, 2)); print_map(mp); } int main() { text(); return 0; } ``` 最后修改:2021 年 09 月 22 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果文章有用,请随意打赏。