map小列

// 有关学生信息的头文件student.h代码如下
#include
#include
using namespace std;
struct Student                                             // 表示学生信息的结构体
{
    string id;                                             // 学号
    string name;                                           // 姓名
    int grade;                                             // 年级
    int cls;                                               // 班级
    Student(){}
    Student( string id, string n, int g, int c )           // 构造函数
        : id( id ), name( n ), grade( g ), cls( c )
    { }
    friend istream & operator >> ( istream &, Student & );    // 友元输入函数
    friend ostream & operator << ( ostream &, const Student &);// 友元输出函数
};
istream &
operator >> ( istream &is, Student &stu )                  // 输入函数
{
    is >> stu.id;
    if( stu.id == "-1" )                   // 如果输入学号为-1,则表示结束输入
    {
      is.setstate( ios_base::failbit );                    // 设置输入流状态
      return is;                                           // 返回
   }
    is >> stu.name;                                        // 输入姓名
    is >> stu.grade;                                       // 输入年级
    is >> stu.cls;                                         // 输入班级
    return is;                                             // 返回输入流
}
ostream &
operator << ( ostream &os, const Student  &stu)             // 输出函数
{
    os << stu.id << ' ';                                   // 输出学号
    os << stu.name << ' ';                                 // 输出姓名
    os << stu.grade << ' ';                                // 输出年级
    os << stu.cls << ' ';                                  // 输出班级
    return os;                                             // 返回输出流
}
//////////////////////////////////////////////////////////////////////////
//主程序文件main.cpp代码如下
#include
#include
#include
#include                                 // 包含头文件map
#include "student.h"                          // 包含学生信息头文件student.h
using namespace std;                          // 使用名称空间std
typedef map STUDENT_BOOK;                 // 定义学生名册类型
typedef map::iterator STU_ITER;           // 定义学生名册迭代器类型
typedef map::const_iterator CONST_STU_ITER;
                                                           // 定义学生名册常量迭代器类型
void outputStudentBook( const STUDENT_BOOK & book )// 输出学生名册的函数
{
  CONST_STU_ITER iter = book.begin();                      // 定义迭代器,指向容器头
     while( iter != book.end() )                            // 遍历学生名册
     {
        cout<< iter->second << endl;                       // 输出学生信息
        iter++;
     }
}
int main(int argc, char *argv[])                           // 主函数
{
     // ——使用map容器管理学生名册——
     STUDENT_BOOK stuBook;                                  // 定义学生名册
     STU_ITER iter;                                         // 定义学生名册迭代器
     cout<<"——建立学生名册——"<<endl;
     cout<<"##输入-1退出##"<<endl;
     Student stu;                                           // 定义学生信息对象
  cin>>stu;                                                // 输入学生信息
     while ( cin )                                          // 输入流状态正确(学号!=-1)
     {
        stuBook[ stu.id ] = stu;                           // 插入学生信息
        cin>>stu;                                          // 继续输入学生信息
     }
     cin.clear();                                           // 恢复输入流状态
     cout<<endl<<"——学生名册——"<<endl;
     outputStudentBook( stuBook );                          // 输出学生名册
     cout<<endl<<"——查找学生——"<<endl;
     cout<<"##请输入学号,输入-1退出##"<<endl;
     string id;
     cin>>id;                                               // 输入要查找的学号
     while( "-1" != id )                                    // 学号 != -1
     {
        iter = stuBook.find( id );                         // 查找学生
        if( iter == stuBook.end() )                        // 如果未找到
        {
            cout<<"未找到学号为"<<id<<"的记录"<<endl;
        }
        else
        {
            cout<<"学生:"<< iter->second <<endl;          // 输出学生信息
        }
        cin>>id;                                           // 继续输入学号
     }
     cout<<endl<<"——删除学生记录——"<<endl;
     cout<<"##请输入学号,输入-1退出##"<<endl;
     cin>>id;                                               // 输入要删除的学号
     while( "-1" != id )                                    // 学号 != -1
     {
        iter = stuBook.find( id );                         // 查找学生
        if( iter == stuBook.end() )                        // 如果未找到
        {
            cout<<"未找到学号为"<<id<<"的记录"<<endl;
        }
 
 else
        {
            stuBook.erase( id );                           // 删除学生信息
        }
        cin>>id;
    }
    cout<<endl<<"——学生名册——"<<endl;
    outputStudentBook( stuBook );                          // 输出学生名册
    system("PAUSE");
    return EXIT_SUCCESS;
}
 


原文地址:https://www.cnblogs.com/tangjunjun/p/11676483.html