首先注意一点
只要出现mp[x],不管x有没有都会新出来一个地址,初始值为0,所以要注意防止插入的内容过多
实验
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<map> #include<cstdlib> using namespace std; map<int,int> mp; int main() { printf("\n%d",mp.size()); if (mp[0]==0) printf("t");else printf("f"); printf("\n%d",mp.size()); return 0; } output 0 t 17、map的基本操作函数: C++ Maps是一种关联式容器,包含“关键字/值”对 begin() 返回指向map头部的迭代器 clear() 删除所有元素 count() 返回指定元素出现的次数 empty() 如果map为空则返回true end() 返回指向map末尾的迭代器 equal_range() 返回特殊条目的迭代器对 erase() 删除一个元素 find() 查找一个元素 get_allocator() 返回map的配置器 insert() 插入元素 key_comp() 返回比较元素key的函数 lower_bound() 返回键值>=给定元素的第一个位置 max_size() 返回可以容纳的最大元素个数 rbegin() 返回一个指向map尾部的逆向迭代器 rend() 返回一个指向map头部的逆向迭代器 size() 返回map中元素的个数 swap() 交换两个map upper_bound() 返回键值>给定元素的第一个位置 value_comp() 返回比较元素value的函数
