关于C++map的简单使用

关于C++map的简单使用

       C++的map属于C++STL的成员,但不知道为什么,很多的C++程序猿们,对于C++的map都不太使用,而作为小菜菜的我本着对知识的虔诚,对这个知识点进行一些总结,以下是代码和注释:

//简单的电话薄操作实例 
#include <iostream>
#include <map>
#include <string>g
using namespace std;
int main(){
	map<string, int> mp; //对map进行实例化 
	int i = 0;
	string name;
	int tel;
	while (i < 3){
		cin >> name >> tel;
		mp.insert(pair<string, int>(name, tel));	//插入数据操作 
		i++;
	}
	map<string, int>::iterator p;				//和其他容器一样,map也可以用迭代器来访问 
	cout << "你输入的数据如下:" << endl;
	for (p = mp.begin(); p != mp.end(); p++){	// map容器数据的输出 
		cout << p->first << p->second << endl;
	}
	cout << "请输入你要删除的姓名:"; 
	cin >> name;
	//map作为一个按照第一个关键字进行索引的有序集合,因此,它的find()的参数是它的第一个索引的值 
	//在find执行完毕后,若找到这个值,该迭代器就会指向该值,否则,就会等于该map对象的end() 
	p = mp.find(name);						                             
	if (p==mp.end()) cout << "Not found!\n";
	else {
		// map的删除函数 
		mp.erase(p);
	} 
	for (p = mp.begin(); p != mp.end(); p++){
		cout << p->first << p->second <<endl;
	}
	return 0;
}
    以上就是本人对map的简单用法的总结,若有不足之处,欢迎指出;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值