头歌“动态学生信息管理” C++、STL

class student
{

public:
    string name;//学生姓名
    int score;//学生分数
    student(string na = "", int sco = 0) :name(na), score(sco)
//使用构造函数的初始化列表给类的成员赋值
    {
        
    }
       bool operator ==(const student & other)const
    {
        return other.name == name;//将“==”运算符重载使得find能够对类的对象进行查找

    }
    
};  
  bool cmp(student  a, student b)
{
    return a.name < b.name;

}

 vector<student> stu1;//定义一个以student类类型的向量容器。

while (scanf("%c",&m) != EOF)//通过该语句实现反复输入

//我目前没找到c++的相关方法(求求大佬教我),注意这个语句需要加入#include<stdio.h>头文件

switch(m)

case 'A':{}break;

case 'R':{}break;

case 'P':{}break;

case 'S':{}break;//通过switch语句使得代码简洁,易懂。

 case 'P':
        {
            if (stu1.empty())
            {
                cout << "[空]" << endl;//如果容器为空,打出“[空]”
            }
            else
            {
                for (auto it = stu1.begin(); it != stu1.end(); it++)
                {
                    cout << it->name << " " << it->score << endl;//通过迭代器的使用对vector进行遍历输出
                }

            }
        }break;
case 'A':
        {

            string name;//定义一个字符串
            int score;//分数
            //暂时存储数据
            cin >> name >> score;
            student stu(name, score);//调用构造函数
            
            if (stu1.empty())//容器为空时,先存入stu1中
            {
                stu1.push_back(stu);
               
            }
            else
            {
              //容器不为空时判断输入的元素是否内的vector成员重名
               auto it =find(stu1.begin(), stu1.end(), name) //要使用find查找时需要对==进行重载
                
            if (it!= stu1.end())//如果发现重复,则只改变该对象的分数,不再增加对象
            {
                it->score = score;
                break;
            }

            else
            {
                stu1.push_back(stu);//如果不重复,则把该对象加入vector
            }

            }
           //方法二:
           /* int flag=0;//设置标志变量,判断是否找到重复
             for (auto it = stu1.begin(); it != stu1.end(); it++)//也可以通过该方案查重,不过也需在判断前就已经有元素在vector内
            {
                if (it!= stu1.end())
                {
                    it->score = score;
                    flag = 1;

                }
             
            }
            if (flag == 1)
            {

            }
            else
            {
               stu1.push_back(stu);
            }*/
           
        }break;
       
case 'R' :
        {
            string name;
            cin >> name;
             auto it =find(stu1.begin(), stu1.end(), name);//查找是否有该元素,存在则返回该元素的迭代器,不存在则返回stu1.end();
             if(it=stu1.end())
             {
                 stu1.erase(it);
             }
          

        }break;

 case 'S':
        {
            sort(stu1.begin(), stu1.end(), cmp);//通过sort对该vector<student>进行排序
//这里需要注意stu.begin()~stu.end()都指向的是一个类,所以需要重新定义一下排序准则
//这里通过sort的第三个参数cmp实现

        }break;

bool cmp(student  a, student b)//这里相当于自己定义了一个比较的方法

    return a.name < b.name;

}

全部代码如下: 

#include <iostream>
#include <vector>
#include <string>
#include<stdio.h>
#include <algorithm>
using namespace std;

/********* Begin *********/

//自定义的类或者其他内容
class student
{

public:
    string name;
    int score;
    student(string na = "", int sco = 0) :name(na), score(sco)
    {

    }
    bool operator ==(const student& other)const
    {
        return other.name == name;

    }

};
bool cmp(student  a, student b)
{
    return a.name < b.name;

}
/********* End *********/

int main()
{
    /********* Begin *********/
    vector<student> stu1;
    int i = 0;
    char m;
    while (scanf("%c", &m) != EOF)
    {
        switch (m)
        {
        case 'A':
        {

            string name;
            int score;

            cin >> name >> score;
            student stu(name, score);
            if (stu1.empty())
            {
                stu1.push_back(stu);
            }
            else
            {
                auto it = find(stu1.begin(), stu1.end(), name);
                if (it != stu1.end())
                {
                    it->score = score;
                    break;
                }
                else
                {
                    stu1.push_back(stu);
                }

            }

            /* for (auto it = stu1.begin(); it != stu1.end(); it++)
             {
                 if (it->name == name)
                 {
                     it->score = score;
                     flag = 1;

                 }

             }
             if (flag == 1)
             {

             }
             else
             {
                stu1.push_back(stu);
             }*/

        }break;
        case 'P':
        {
            if (stu1.empty())
            {
                cout << "[空]" << endl;
            }
            else

            {
                for (auto it = stu1.begin(); it != stu1.end(); it++)
                {
                    cout << it->name << " " << it->score << endl;
                }

            }
        }break;
        case 'R':
        {
            string name;
            cin >> name;
            auto it = find(stu1.begin(), stu1.end(), name);
            if (it!=stu1.end())
            {
                stu1.erase(it);
            }


        }break;
        case 'S':
        {
            sort(stu1.begin(), stu1.end(), cmp);

        }break;
        }
    }
    /********* End *********/
}

 总结:

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值