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 *********/
}
总结:
- vector可以定义成vector<类名>
- sort,find在使用时需要分别重载“>”和“==”
- 注意迭代器失效的报错。这里推荐去看这篇文章:(24条消息) 关于迭代器失效的几种情况_Shining-LY的博客-CSDN博客z
- 赋值时,不知道左值应该为什么类型,可以用auto