//建立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未出现编译错误?