#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");
}