1、map的删除:
删除的话首当其冲的肯定是erase方法了。erase 方法支持 key 删除和迭代器删除,例如:
stud_sex_map.erase(10010);
stud_sex_map.erase(stud_sex_map.begin());
2、map的遍历:
因为是 map 不是数组,所以不能用下标来遍历,只能用迭代器来遍历,如下:
for (map<int, char>::iterator itor = stud_sex_map.begin(); itor != stud_sex_map.end(); ++itor)
{
int key = itor->first;
char ch = itor->second;
cout << "key = " << key << ", value = " << ch << endl;
}
int main(int argc, char* argv[])
{
map<int, char> stud_sex_map;
stud_sex_map[10010] = 'm'; //赋值
stud_sex_map[10011] = 'f';
int n_size = stud_sex_map.size(); //返回容器的元素个数
stud_sex_map.empty(); //检测容器是否为空
char sex = stud_sex_map[10010];
sex = stud_sex_map[10012]; //这个值会是多少呢?
if (stud_sex_map.count(10012) <= 0) //判断容器中是否有某个元素
{
stud_sex_map[10012] = 'f';
}
sex = stud_sex_map[10012];
return 0;
}