64,686
社区成员
发帖
与我相关
我的任务
分享
class student
{
public:
student(){}
student(string name,int age)
{
this->name=name;
this->age=age; //1.露了=age
}
string GetName()
{ return name;}
int GetAge() const //2.露了const
{ return age;}
private:
string name;
int age;
};
bool comp(const student &s1,const student &s2)
{
return s1.GetAge() <s2.GetAge();
}
int main()
{
vector<student> vs;
vs.push_back(student("zhang",19));
vs.push_back(student("wang",12));
vs.push_back(student("li",21));
vs.push_back(student("sun",6));
cout <<(*vs.begin()).GetName() <<endl;
cout <<vs.back().GetName() <<endl;
cout <<"年龄最大的学生:" <<max_element(vs.begin(),vs.end(),comp)->GetName() << endl;
return 0;
}
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
class student
{
public:
student(){}
student(string name,int age)
{
this->name=name;
this->age;
}
string GetName()
{ return name;}
int GetAge() const
{ return age;}
private:
string name;
int age;
};
bool comp(const student &s1,const student &s2)
{
return s1.GetAge() < s2.GetAge();
}
int main()
{
vector <student> vs;
vs.push_back(student("zhang",19));
vs.push_back(student("wang",12));
vs.push_back(student("li",21));
vs.push_back(student("sun",6));
cout <<(*vs.begin()).GetName() <<endl;
cout <<vs.back().GetName() <<endl;
cout <<"年龄最大的学生:" <<max_element(vs.begin(),vs.end(),comp)->GetName() <<endl;
system("pause");
return 0;
}