#include<map>#include<string>#include<iostream>usingnamespace std;template<typename T>voidshow(const T& a){for(auto i = a.begin(); i != a.end(); i ++)
cout << i->first<<" -> "<< i->second << endl;
cout << endl;}// 定义 降序排序 template<typename T>struct SortDescending{booloperator()(const T& a,const T& b){return( a > b);}};intmain(){//降序的map
map <int, string, SortDescending<int>> a1;// or map<int,string,greater<int>> a1;// 四种插入方法
a1.insert( map <int, string>::value_type (3,"Three"));
a1.insert(make_pair(1,"One"));
a1.insert( pair <int, string>(2,"Two"));
a1[4]="Four";
cout <<"a1.size() is "<< a1.size()<< endl;show( a1);
multimap <int, string>a2( a1.begin(), a1.end());
a2.insert(make_pair(4,"four"));
cout <<"a2.size() is "<< a2.size()<< endl;show( a2);//a2.count( key) 返回值为 key 的键值对 对数
cout <<"the number of pairs with the key 4 is "<< a2.count(4)<< endl;}
#include<map>#include<iostream>#include<string>usingnamespace std;template<typename T>voidshow(const T& a){for(auto p = a.begin(); p != a.end(); p ++)
cout << p -> first <<" -> "<< p -> second << endl;
cout << endl;}intmain(){
map <int, string> a;
a.insert(make_pair(1,"One"));
a.insert( pair<int, string>(3,"Three"));
a.insert( map <int, string>:: value_type (2,"Two"));
a[4]="Four";show( a);int x;
cout <<"enter the key you want to search"<< endl;
cin >> x;// a.find() returns a iteratorauto i = a.find( x);if( i != a.end()) cout << i -> first <<" -> "<< i -> second << endl;else cout <<"not found"<< endl;}
#include<map>#include<string>#include<iostream>usingnamespace std;template<typename T>voidshow(const T& a){for(auto p = a.begin(); p != a.end(); p ++)
cout << p -> first <<" -> "<< p -> second << endl;
cout << endl;}intmain(){
multimap <int, string> a;
a.insert(make_pair(1,"One"));
a.insert(make_pair(1,"ONE"));
a.insert(make_pair(1,"ONe"));
a.insert(make_pair(1,"OnE"));show( a);int key;
cout <<"enter the key you want to search"<< endl;
cin >> key;auto p = a.find( key);if( p != a.end()){int x = a.count( key);for(int i =0; i < x; i ++){
cout <<"key "<< p -> first <<", value["<< i <<"] is "<< p -> second << endl;
p ++;}}else cout <<"the key is not found"<< endl;}
#include<map>#include<string>#include<iostream>usingnamespace std;template<typename T>voidshow(const T& a){for(auto p = a.begin(); p != a.end(); p ++)
cout << p -> first <<" -> "<< p -> second << endl;
cout << endl;}intmain(){
multimap <int, string> a;
a.insert( make_pair (1,"One"));
a.insert( make_pair (1,"ONe"));
a.insert( make_pair (1,"ONE"));
a.insert( make_pair (2,"Two"));
a.insert( make_pair (2,"TWO"));
a.insert( make_pair (3,"Three"));
a.insert( make_pair (4,"Four"));
a.insert( make_pair (5,"Five"));show( a);//1 a.erase( key) int num = a.erase(1);
cout <<"erase "<< num <<" pairs"<< endl;show( a);//2 a.erase( iterator)auto p = a.find(2);// 只删除了迭代器指向的元素 删除了1个元素 if( p != a.end()) a.erase( p);show( a);//3 a.erase( a.lower_bound( key1), a.upper_bound( key2))
a.erase( a.lower_bound(3), a.upper_bound(5));show( a);}
#include<map>#include<string>#include<algorithm>#include<iostream>usingnamespace std;template<typename T>voidshow(const T& a){for(auto p = a.begin(); p != a.end(); p ++)
cout << p -> first <<" -> "<< p -> second << endl;
cout << endl;}//定义 不区分大小写的 排序 struct SortDescending{booloperator()(const string& a,const string& b)const{
string s1( a),s2( b);transform( s1.begin(), s1.end(), s1.begin(),::tolower);transform( s2.begin(), s2.end(), s2.begin(),::tolower);return( s1 < s2);}};intmain(){// map a 插入时、搜索时 均不区分大小写
map <string, string, SortDescending> a;
a.insert( make_pair ("John","135250"));
a.insert( make_pair ("JOHN","135250"));
a.insert( make_pair ("Joey","1823613"));
a.insert( make_pair ("Ross","2224830"));show( a);
map <string, string>b( a.begin(), a.end());show( b);
cout <<"In map a: enter the name you want to search"<< endl <<'>';
string s1;
cin >> s1;auto p = a.find( s1);if( p != a.end()) cout << p -> first <<"'s number is "<< p -> second << endl;else cout <<"not found"<< endl;
cout << endl;
cout <<"In map b: enter the name you want to search"<< endl <<'>';
string s2;
cin >> s2;auto i = b.find( s2);if( i != b.end()) cout << i -> first <<"'s number is "<< p -> second << endl;else cout <<"not found"<< endl;}