map
map 的意思是映射。用法一般是
map<char, int>mp
按照我的理解,map 类似于一个高级的数组。前面的数据类型 char 相当于下脚标,而数组元素的值就对应着后面的类型 int。例如可以用一个 map<string, int>month_name 来表示“月份名字对应的月份编号“。然后用 month_name["july"] = 7 这样的方式来赋值。所以说,”高级的数组“指下脚标和元素类型可以是任意数据类型的(当然包括结构体)。
接下来说说基本操作
1.声明,赋值
除了上面已经讲过的像数组元素那样赋值的方法, 还支持这样 mp.insert(make_pair(”july“, 7)); (不过当然是上面的方法简单)
2.查找
和 set 很像:if (mp.find('f') == mp.end()) ......,若找不到,就执行 if 里面的语句。(找的是下脚标,不是元素的值)
3. 输出大小
同理 set :mp.size()
4.遍历
1 #include <bits/stdc++.h> 2 using namespace std; 3 map<char, int>mp; 4 int main() 5 { 6 for (int i = 0; i < 5; i++) mp['a' + i] = i; 7 for (map<char, int>::iterator it = mp.begin(); it != mp.end(); ++it) 8 printf("%c %d\n", it -> first, it -> second); 9 /*若嫌麻烦,也可在前面用 typedef 改个名,以后遍历就不用写一串了 10 typedef map<char, int>::iterator mcit; 11 for(mcit it = mp.begin(); it != mp.end(); ++it) */ 12 return 0; 13 }
输出:
a 0
b 1
c 2
d 3
e 4
还有 first 就是指“下脚标”, second 指”数组元素值“。
5.删除
和 set 一样
mp.erase('f'); 或 map<char, int>::iterator it = mp.find('f'); mp.erase(it);
6. lower_bound() 和 upper_bound()
map<char, int>::iterator it1 = mp.lower_bound('f'); printf("%c %d\n", it1->first, it1->second); 就这样
7.!注意 !
看这段代码
1 #include <bits/stdc++.h> 2 using namespace std; 3 map<char, int>mp; 4 int main() 5 { 6 for (int i = 0; i < 5; i++) mp['a' + i] = i; 7 cout << mp['z'] << endl; 8 for (map<char, int>::iterator it = mp.begin(); it != mp.end(); ++it) 9 printf("%c %d\n", it -> first, it -> second); 10 return 0; 11 }
在第7行,我们发现 mp['z'] 根本没有,然而这是能过编译的!而且输出时,结果是这样的:
0
a 0
b 1
c 2
d 3
e 4
z 0
可见 mp['z'] 的值是0。而且这个 mp['z'] 也被自动添加到了 mp 中,看来c++是自己在 mp 中创建了一个 mp['z']。所以我们凡是要对 map 进行操作时,都要首先确认这个元素是否存在,否则就会出现这些像意想不到的错误。
multimap
multimap的用法几乎map一样,唯一不同的是,它里面的元素是可以重的。
所以两种删除方法在这里就不一样了:
1.mp.erase('f'):删除multimap中所有以'f'为下标的元素。
2. map<char, int>::iterator it = mp.find('f'); erase(it):只删除第一个‘’f.
应用
1.map可以代替hash
虽然复杂度多了一个log,但是在时间充裕而且代码比较长的情况下,用map写就显得极为方便。
举一个很水的例子:洛谷P3370 【模板】字符串哈希
1 #include<cstdio> 2 #include<string> 3 #include<map> 4 using namespace std; 5 6 map<string, bool> mp; 7 int n, cnt = 0; 8 char s[1505]; 9 10 int main() 11 { 12 scanf("%d", &n); 13 for(int i = 1; i <= n; ++i) 14 { 15 scanf("%s", s); 16 if(!mp[s]) mp[s] = 1, cnt++; 17 } 18 printf("%d\n", cnt); 19 return 0; 20 }