读书人

关于stl的查找对象有关问题

发布时间: 2012-03-14 12:01:13 作者: rapoo

关于stl的查找对象问题
准备用slt写一个成绩登记, 排序, 查找, 删除的程序。
请问用slt怎么查找和删除一个对象阿
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <string>
using namespace std;

class student
{
public:
student(const string &a, int b):name(a), score(b) {}
string name;
string num;
int score;
bool operator < (const student &m)const
{
return score< m.score;
}
};

int main()
{
vector< student> vect;
student st1("0", 0);
string str;
int intscore;
while(1)
{
cout << "1.newdate\nB.back\n";
cin >> str;
string tstr;
int intscore;
if (str[0] == '1')
{
cout << "Student's num: ";
cin >> st1.num;
cout << "Student's name: ";
cin >> tstr;
cout << "Sore: ";
cin >> intscore;
st1.name=tstr;
st1.score=intscore;
vect.push_back(st1);
}
else if(str[0] == 'B') break;
}
stable_sort(vect.begin(), vect.end(),less<student>());
cout <<"-----after sort ...."<<endl;
for(int i = 0 ; i < vect.size(); i ++) cout<<vect[i].name<<":\t"<<vect[i].score<<endl;
return 0 ;
}




[解决办法]
重载==
[解决办法]
algorithm已经有合适的函数了,你看看吧

读书人网 >C++

热点推荐