yang970974062 2016-12-24 09:02 采纳率: 0%
浏览 1043

C++STL中 stable_sort()函数BUG!!帅哥们求helping

#define _CRT_SECURE_NO_WARNINGS
#include
using namespace std;
#include
#include
#include

template

class Stduent
{
public:
Stduent(char p, int age)
{
this->p_name = new char[strlen(p) + 1];
strcpy(this->p_name, p);
this->age = age;
}
Stduent(const Stduent&obj)

{
this->p_name = new char[strlen(obj.p_name) + 1];
strcpy(this->p_name, obj.p_name);
this->age = obj.age;//重载copy函数是因为副本要拷贝 防止副本和原来都指向同一块1内存
}
void print()
{
cout << "p_name " << this->p_name << endl;
}
int return_()
{
return this->age;
}
//重载= 是因为 为了支持泛型编程
Stduent& operator=(const Stduent&other)
{
/*Stduent s4("hello", 1);
s2=s4 = s3;
=号操作而不是初始化操作 所以this-指向的内存必须先释放掉
/
if (this->p_name != NULL)
{
delete[] this->p_name;
this->p_name = NULL;
this->age = 0;
}

    this->p_name = new char[strlen(other.p_name) + 1];
    strcpy(this->p_name, other.p_name);
    this->age = other.age;

    return *this;
}

public:
char *p_name;
int age;
};
bool temping( Stduent& t1,const Stduent& t2)
{
if (t1.age > t2.age)
{
return true;
}else
{
return false;
}
}

void main()
{

Stduent<int>  s1("悦儿", 19);
Stduent<int>  s2("杨阳", 21);
Stduent<int>  s3("杨依", 0);
Stduent<int>  s4("hello", 1);
s4 = s3;

vector<Stduent<int>>   v1;
v1.push_back(s1);
v1.push_back(s2);
v1.push_back(s3);
v1.push_back(s4);

for (vector<Stduent<int>>::iterator it = v1.begin(); it != v1.end(); it++)
{

    it->print();
}
    // 下面是函数对象!!!
stable_sort(v1.begin(), v1.end(), ffc());//error :第三个参数出错




system("pause");

}

  • 写回答

1条回答 默认 最新

  • zqbnqsdsmd 2016-12-24 15:49
    关注
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料