首页 > 其他 > 详细

map

时间:2017-12-18 22:43:20      阅读:328      评论:0      收藏:0      [点我收藏+]

map 的遍历 

代码示例

#include <map>  
#include <string>  
#include <iostream>  
using namespace std;  
int main()  
{  
       map<int, string> mapStudent;  
       mapStudent.insert(pair<int, string>(1, "student_one"));  
       mapStudent.insert(pair<int, string>(2, "student_two"));  
       mapStudent.insert(pair<int, string>(3, "student_three"));  
       map<int, string>::iterator iter;  // 迭代器 
    for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++){
      printf("%d %d\n", iter->first, iter->second); // 直接引用两个元素
    }       
}  

判断一个元素是否出现过

mp.count( x );

当 x 出现过,函数的返回值为 1 , 当没有出现过,函数的返回值为 0 。

 

删除元素 

map<int, int>::iterator it;

mp.erase( it ) , 删除后 it 所指向的地址是删除元素的下一个地址 。 好坑啊这里 !!

map

原文:http://www.cnblogs.com/ccut-ry/p/8058989.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!