我调用max_element哪出错了

ycm_113 2008-03-29 04:39:28
#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()
{ 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;
}
这个程序编译不能通过,我把comp函数的const去掉,就能通过了,为什么?
另外,去掉const后输出的结果也不正确?请高手指点一下我这个程序哪里错了
...全文
272 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hastings 2008-03-29
  • 打赏
  • 举报
回复
正是由于C++中的成员函数有const和非const的区别,
所以才有写时复制的技术.VC6.0的string类就是写时复制.
hastings 2008-03-29
  • 打赏
  • 举报
回复
顶5楼,典型的const问题;
LZ在写成员函数时,若它不会改变成员,则最好加上const,
有时候同一成员函数需要两个版本,const版本和非const版本.
如重载*和->时.
ttkk_2007 2008-03-29
  • 打赏
  • 举报
回复

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;
}
paidfighting 2008-03-29
  • 打赏
  • 举报
回复
粗心的lz

构造函数:

student(string name,int age)
{
this->name = name;
this->age = age;
}
ryfdizuo 2008-03-29
  • 打赏
  • 举报
回复
貌似没什么问题了,或者用sort函数排序,
paidfighting 2008-03-29
  • 打赏
  • 举报
回复

#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;
}
paidfighting 2008-03-29
  • 打赏
  • 举报
回复
给GetAge加上const也可以

const对象只能调用const函数

64,686

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧