一 定义 C++中map提供的是一种键值对容器,里面的数据都是成对出现的。每一对中的第一个值称之为关键字(key),每个关键字只能在map中出现一次;第二个称之为该关键字的对应值。
map的底层实现时红黑树,查询的时间复杂度时log(n)
二 声明
三 插入操作 1.通过inset插入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <map> map<int ,string> mymap; mymap.insert (pair <int ,string>(2019 ,"hello world" )); map<int ,string>::iterator it; for (it=mymap.begin ();it!=mymap.end ();it++) { cout << it->first << " " ; cout << it->second ; } cout <<endl;
2.数组插入
1 2 3 4 5 6 7 8 map<string,int > mymap; mymap["hello" ] = 2019 ; cout << mymap.begin ()->first << " " << mymap.begin ()->second; return 0 ;
3.列表插入
1 2 3 4 5 6 7 8 9 10 11 12 13 int main () { map<string,int > mymap; mymap.insert ({"hello" ,2020 },{"world" ,2021 }); map<string,int >::iterator it; for (it = mymap.begin ();it!=mymap.end ();it++) { cout << it->first << " " <<it->second; cout <<endl; } return 0 ; }
4.指定位置插入(类型为char会按照ascii从小到大排序)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 int main () { map<char ,int >mymap; mymap.insert (pair <char ,int >('a' ,100 )); mymap.insert (pair <char ,int >('b' ,200 )); map<char , int >::iterator it = mymap.begin () ; mymap.insert (it,pair <char ,int >('c' ,105 )); auto iter = mymap.begin (); while (iter!=mymap.end ()) { cout << iter->first << " " << iter->second ; iter++; } return 0 ; }
四 取值 1 2 3 4 5 6 7 8 9 int main () { map<char ,int >mymap; mymap.insert (pair <char ,int >('a' ,100 )); map<char ,int >::iterator iter; iter = mymap.begin (); cout << iter->first << " " << iter->second ; return 0 ; }
五 容量查询 1.查询map是否为空 mymap.empty();
2.查询map中键值对的数量 mymap.size();
3.查询map所能包含的最大键值对数量,和系统和应用库有关。此外,这并不意味着用户一定可以存这么多,很可能还没达到就已经开辟内存失败了 mymap.max_size();
5.查询关键字为key的元素的个数,在map里结果非0即1
mymap.count( const Key& key ) const
mymap.clear() //清空map
六 查找 1 2 3 4 5 iterator find (const key_type& k) ;const_iterator find (const key_type& k) const ;
七 利用返回值检验map的key是否相同 内容不同:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 int main () { map<string,int > mymap; mymap.insert (pair <string,int >("hello world" ,2020 )); pair<map<string,int >::iterator, bool > ret; ret = mymap.insert (pair <string,int >("hello" ,2020 )); if (ret.second == false ) { cout <<"already exists" <<endl; } else { cout << ret.first->first << " " ; cout << ret.first->second; } return 0 ; }
内容相同:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 int main () { map<string,int > mymap; mymap.insert (pair <string,int >("hello" ,2020 )); pair<map<string,int >::iterator, bool > ret; ret = mymap.insert (pair <string,int >("hello" ,2020 )); if (ret.second == false ) { cout <<"already exists" <<endl; } else { cout << ret.first->first << " " ; cout << ret.first->second; } return 0 ; }
八 删除元素 1.通过key值删除
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 int main () { map<string,int >mymap; mymap.insert (pair <string,int >("hello world" ,10 )); mymap.insert (pair <string,int >("good morning" ,100 )); map<string, int >::iterator it; mymap.erase ("good morning" ); for (it = mymap.begin ();it!=mymap.end ();it++) { cout << it->first <<endl; } return 0 ; }
2.删除指定位置的元素
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 int main () { map<string,int >mymap; mymap.insert (pair <string,int >("hello world" ,10 )); mymap.insert (pair <string,int >("good morning" ,100 )); map<string, int >::iterator it; mymap.erase (mymap.find ("good morning" )); for (it = mymap.begin ();it!=mymap.end ();it++) { cout << it->first <<endl; } return 0 ; }
九 交换 1 2 void swap ( map& other ) ;
十 题目 HDU2648
看到输入样例有数字 => 字符串的格式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 #include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <stack> #include <set> #include <list> #include <map> using namespace std;typedef long long ll;int main () { int n,m,p[10000 ]; string s; map<string,int > shop; while (cin >> n) { for (int i=0 ;i<n;i++) { cin >> s; shop[s] = 0 ; } int days; cin >> days; while (days--) { int ranks = 1 ; for (int i=1 ;i<=n;i++) { cin >> m >> s; shop[s] += m; p[i] = shop[s]; } for (int i=1 ;i<=n;i++) { if (p[i]>shop["memory" ]) ranks++; } cout << ranks <<endl; } } return 0 ; }
罗马数字转数字
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include <iostream> #include <set> #include <queue> #include <map> #include <stack> #include <cstring> using namespace std;int romanToInt (string s) { map<char ,int > roman_map{{'I' ,1 },{'V' ,5 },{'X' ,10 },{'L' ,50 },{'C' ,100 },{'D' ,500 },{'M' ,1000 }}; int pre = roman_map[s[0 ]]; int ans = 0 ; for (int i=1 ;i<s.length (); ) { int cur = roman_map[s[i]]; if (pre >= cur) { ans = ans + pre; pre = cur; i++; } else { ans = ans + cur - pre; pre = roman_map[s[i+1 ]]; i = i+2 ; } } ans = ans + pre; return ans; } int main () { string s; cin >> s; int c = romanToInt (s); cout << c <<endl; return 0 ; }