学习笔记:STL模板中map的使用

//建立map容器保存学生姓名和密码,输出所有学生信息,并根据姓名查找相应的密码

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string>
#include <map>
using namespace std;
int main()
{
	cout << "Hello World!\n";
	map<string, int> studentInfo;
	studentInfo["liyi"] = 123;		//键——键值
	studentInfo["WangYong"] = 456;
	studentInfo["XuWan"] = 789;

	//输出所有学生信息
	map<string, int>::iterator it;
	for (it = studentInfo.begin(); it != studentInfo.end(); it++)
	{
		cout << it->first << " " << it->second << endl;

	}
	//查询某个人的密码
	string name;
	cout << "请输入学生的姓名:";
	cin >> name;
	//it = NULL;						//为什么该语句不能编译通过
	//if ((it = studentInfo.find(name)) == NULL)
	//{
	//	cout << "没有此人" << endl;
	//}
	//else
	//{
	//	cout << "此人的密码是:" << it->second << endl;
	//}

	getchar();
}

运行结果:

存在问题:

为什么迭代器it = NULL未出现编译错误?

发布了34 篇原创文章 · 获赞 1 · 访问量 727

猜你喜欢

转载自blog.csdn.net/qq_41708281/article/details/104178357