关联式容器map/multimap之元素个数统计、查找元素及元素的随机访问

来源:互联网 发布:sai for mac 有压感 编辑:程序博客网 时间:2024/05/22 05:27

#include <iostream>#include <map>using namespace std;typedef pair<int, double> mypair;map<int, double, less<int>>::iterator it1;multimap<int, double, less<int>>::iterator it2;void print(map<int, double, less<int>> &);void printM(multimap<int, double, less<int>> &);void main(){map<int, double, less<int>> m1;multimap<int, double, less<int>> m2;mypair temp;typedef map<int, double, less<int>> Map;typedef multimap<int, double, less<int>> MMap;// 初始化容器,插入操作m1.insert(mypair(1, 6.0));m1.insert(mypair(2, 13.5));m1.insert(mypair(3, 11.3));m1.insert(mypair(4, 13.4));m1.insert(mypair(5, 15.1));m2.insert(mypair(1, 2.3));m2.insert(mypair(2, 3.0));m2.insert(mypair(2, 10.6));m2.insert(mypair(3, 9.4));m2.insert(mypair(4, 15.3));// 输出容器内容cout << "m1:" << endl;print(m1);cout << "m2:" << endl;printM(m2);cout << endl;// -------------- 元素个数的统计 ------------------------/* Count()函数的功能是返回元素在容器Map/Multimap中出现的次数。函数原型是:size_type count(const key_type& _Keyval) const;*/int size = m1.count(2);int Msize = m2.count(2);cout << "map里面键值为2的元素个数:" << size << endl;cout << "multimap里面键值为2的元素个数:" << Msize << ";" << endl;cout << endl;// ----------------- 查找元素 ---------------------------/* 容器Map/Multimap实现查找元素功能的成员函数是find()。其功能是返回指向key的迭代器。函数原型是:iterator find(const key_type& _Keyval);const_iterator find(const key_type& _Keyval) const;*/it1 = m1.find(5);// WRONG-> it1 = find(5);// map<int, double, less<int>>::iterator it1;cout << "m1中键值为5的元素对为" << endl;cout << (*it1).first << ";" << (*it1).second << endl;/* mypair temp;temp = *it1;cout << "m1中键值为5的元素对为" << endl;cout << temp.first << ";" << temp.second << endl;*/it2 = m2.find(2);cout << "m2中键值为2的元素对为" << endl;cout << it2->first << "; " << it2->second << endl;cout << endl;// ----------------- 元素的随机访问 ---------------------/* 容器Map/Multimap元素的随机访问功能包括函数upper_bound()、lower_bound()和equal_range():Iterator lower_bound(const Key &key);Const_iterator lower_bound(const Key& key) const;Pair<iterator, iterator> equal_range(const Key &key);Iterator upper_bound(const Key &key);const_iterator upper_bound(const Key &key) const;*/cout << "map m1中键值为2的元素对为:" << endl;// 迭代器 ------------ lower_bound ---------it1 = m1.lower_bound(2);// 总是忘记写m1.   ̄□ ̄||temp = *it1;cout << temp.first << ";" << temp.second << endl;cout << "multimap m2中键值为2(lower_bound)的元素对为:" << endl;it2 = m2.lower_bound(2);cout << it2->first << ";" << it2->second << endl;// --------------- upper_bound ------------------cout << "map m1中键值为2的元素对为:" << endl;it1 = m1.upper_bound(2);temp = *it1;cout << temp.first << ";" << temp.second << endl;cout << "multimap m2中键值为2(upper_bound)的元素对为:" << endl;it2 = m2.upper_bound(2);cout << it2->first << ";" << it2->second << endl;cout << endl;// --------------- equal_bound ------------------pair<Map::iterator, Map::iterator> p1;pair<MMap::iterator, MMap::iterator> p2;p1 = m1.equal_range(3);cout << p1.first->second << "; " << p1.second->second << endl;p2 = m2.equal_range(2);cout << p2.first->second << "; " << p2.second->second << endl;cin.get();}void print(map<int, double, less<int>> &m){map<int, double, less<int>>::iterator it;mypair tmp;if (m.size() < 0)cout << "Map is empty!" << endl;else{for (it = m.begin(); it != m.end(); it++){tmp = *it;cout << tmp.first << ";" << tmp.second << endl;}}}void printM(multimap<int, double, less<int>> &m){multimap<int, double, less<int>>::iterator it;mypair tmp;if (m.size() < 0)cout << "Map is empty!" << endl;else{for (it = m.begin(); it != m.end(); it++){tmp = *it;cout << tmp.first << ";" << tmp.second << endl;}}}


编译结果:


0 0