题目:
C++ 面向对象 _ STL 的应用 (educoder.net)
考点:
1.自定义排序 bool cmp
2.如何使用find和erase来找到学生类里面的指定姓名的人并将其从动态数组中删除。
3.find要找的是学生类里面的成员变量而非单纯的直接找值,应如何实现 (与自定义排序有相似之处)。
查找学生类中姓名的实现:
find_if实现:
auto it = find_if(stu.begin(), stu.end(), [na](const student& s) {return s.name == na; })。
其中[targetName](const Student& s) { return s.name == targetName; });是什么作用?
这是一个lambda表达式,用于作为`std::find_if`函数的第三个参数,即谓词(Predicate)。谓词是一个可调用对象,它接受一个参数并返回一个bool值,用于指示该参数是否满足某个条件。`std::find_if`函数会在指定的范围内查找第一个使谓词返回true的元素,并返回指向该元素的迭代器。如果没有找到,则返回`last`迭代器。
在这个示例中,我们的lambda表达式接受一个`const Student&`类型的参数`s`,并返回`s.name == targetName`的比较结果,即判断该学生的姓名是否等于目标姓名`targetName`。这个lambda表达式的作用是用于在`stu`容器中查找名字为`targetName`的学生。
如果找到了符合条件的学生,则将其从容器中删除;如果未找到,则输出未找到的消息。最后,我们打印出剩余的学生信vector:
vector:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
/********* Begin *********/
//自定义的类和者其他内容
class student
{
public:
string name;
int score;
};
vector<student>stu;
void print(vector<student>&stu)
{
if(stu.empty()){cout<<"[空]"<<endl;return;}
for(vector<student>::iterator it=stu.begin();it!=stu.end();it++)
cout<<it->name<<" "<<it->score<<endl;
}
bool cmp(student stu1,student stu2){return stu1.score>stu2.score;}
void Sort(vector<student>&stu){sort(stu.begin(),stu.end(),cmp);}
/********* End *********/
int main()
{
/********* Begin *********/
//读取输入数据,执行动作
char c;
string na;int sc;
while(cin>>c)
{
if(c=='A')
{ student t;cin>>na>>sc;
auto it=
find_if(stu.begin(),stu.end(),[na](const student&s){return s.name==na;});
if(it==stu.end()) {t.name=na;t.score=sc;stu.push_back(t);}
else it->score=sc;
}
if(c=='R')
{cin>>na;
auto it=
find_if(stu.begin(),stu.end(),[na](const student& s){return s.name==na;});
if(it!=stu.end())stu.erase(it);
}
if(c=='P'){print(stu);}
if(c=='S'){Sort(stu);}
}
/********* End *********/
}
list:
#include<iostream>
//#include<vector>
#include<list>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
class stu
{
public:
string name;
int score;
stu(string n="", int s = 0) :name(n), score(s) {}
};
//vector<stu>arr;
list<stu>arr;
void Print(list<stu>&arr)
{
if (arr.size()==0) { cout << "[空]"; return; }
for (list<stu>::iterator it = arr.begin(); it != arr.end(); it++)
{ cout <<it->name << ":" <<it->score<< endl; }
//for (int i = 0; i < arr.size(); i++) { cout << arr[i].name << ":"<<arr[i].score << endl; };
}
void Add(list<stu>&arr)
{
string na; int sco;
cin >> na >> sco;
list<stu>::iterator it =
find_if(arr.begin(), arr.end(), [na](const stu& s) {return s.name == na; });
if (it == arr.end()) { stu student; student.name = na; student.score = sco; arr.push_back(student); return; }
it->score = sco;
}
void Delete(list<stu>&arr)
{
string na;
cin >> na;
auto it = find_if(arr.begin(), arr.end(), [na](const stu& s) {return s.name == na; });
if (it != arr.end())arr.erase(it);
}
bool comp(stu student1, stu student2)
{
return student1.score > student2.score;
}
void Sort(list<stu>&arr)
{
//sort(arr.begin(), arr.end(), comp);
arr.sort(comp);
}
int main()
{
char c;
while (cin >> c) {
if (c == 'P')Print(arr);
else if (c == 'A') Add(arr);
else if (c == 'R')Delete(arr);
else if (c == 'S')Sort(arr);
}
}
Vector和List区分:
Vector和List是C++中的两种容器,它们各自有以下特点:
Vector:
- 底层实现为动态数组,支持随机访问,可以通过下标访问元素。
- 在尾部插入和删除元素的时间复杂度为常数,但在其他位置插入和删除元素的时间复杂度为线性。
- 支持元素的动态增长和缩减,可以通过resize()方法改变容器的大小。
- 内存连续,可以提高缓存命中率,但是在插入和删除元素时需要移动其他元素,效率较低。
List:
- 底层实现为双向链表,不支持随机访问,只能通过迭代器访问元素。
- 在任意位置插入和删除元素的时间复杂度为常数。
- 支持元素的动态增长和缩减,可以通过resize()方法改变容器的大小。
- 内存不连续,不能提高缓存命中率,但插入和删除元素时只需要改变指针指向,效率较高。
总的来说,Vector适合需要随机访问元素或在尾部频繁插入和删除元素的场景,而List适合在任意位置频繁插入和删除元素的场景。