问题描述
- C++ STL中的sort排序算法
-
#define _CRT_SECURE_NO_WARNINGS
#include"iostream"
using namespace std;
#include"vector"
#include"list"
#include"set"
#include"algorithm"
#include"functional"
class teacher
{
public:
int age;
char *name;
public:
teacher()
{
this->name = new char[1];
strcpy(this->name, 0);
this->age = 0;
}
teacher(char *name,int age)
{
this->name = new char[strlen(name) + 1];
this->age = age;
strcpy(this->name, name);
}
teacher(const teacher&obj)
{
this->name = new char[strlen(obj.name) + 1];
this->age =obj.age;
strcpy(this->name, obj.name);
}~teacher() { if (this->age != NULL) { delete[]this->name; } this->age = 0; this->name = NULL; } teacher* operator = (teacher&obj) { teacher* p; p = &obj; return p; } bool operator<(const teacher obj5) { return this->age < obj5.age; }
};
bool chang(teacher obj1, teacher obj2)
{cout << obj1.name << endl;
if (obj1.age < obj2.age)
{
return true;
}
else
{
return false;
}
}void main02()
{
teacher ouyangdian("ouyangdian", 18);
teacher wuhui("wuhui",25);
teacher wudong("wudong", 19);
teacher zhuwenjun("zhuwenjun", 17);
vector array;
array.push_back(ouyangdian);
array.push_back(wuhui);
array.push_back(wudong);
array.push_back(zhuwenjun);
vector::iterator it = array.begin();
sort(it, array.end(),chang);
vector::iterator it1 = array.begin();
for (it1; it1 != array.end(); it1++)
{
cout << it1->name << " " << it1->age << endl;
}
}void main()
{
//main03();
main02();
system("pause");
return;
}请各位大神看看这段代码,为何不能实现排序呢。。跪求
解决方案
C++之所以得到这么多人的喜欢,是因为它既具有面向对象的概念,又保持了C语言高效的特点。STL 排序算法同样需要保持高效。因此,对于不同的需求,STL提供的不同的函数,不同的函数,实现的算法又不尽相同。
1.1 所有sort算法介绍
所有的sort算法的参数都需要输入一个范围,[begin, end)。这里使用的迭代器(iterator)都需是随机迭代器(RadomAcce......
答案就在这里:STL sort排序算法详细介绍
解决方案二:
vectorarray;
解决方案三:
明明加了为什么上面的回答显示不出来... 重新贴一次, vector必须加元素类型, 你上面的代码能编的过?? 编译都会出错, 怎么会知道无法排序......
vector<teacher> array;
vector<teacher>::iterator;
解决方案四:
代码问题不少,但你没说清问题到底出在哪里是sort排序的结果不对吗
解决方案五:
排序是没问题的,问题在=赋值运算符上,注释掉的话排序就对了
楼主再修改下
#define _CRT_SECURE_NO_WARNINGS
#include"iostream"
using namespace std;
#include"vector"
#include"list"
#include"set"
#include"algorithm"
#include"functional"
class teacher
{
public:
int age;
char *name;
public:
teacher()
{
this->name = new char[1];
strcpy(this->name, 0);
this->age = 0;
}
teacher(char *name, int age)
{
this->name = new char[strlen(name) + 1];
this->age = age;
strcpy(this->name, name);
}
teacher(const teacher&obj)
{
this->name = new char[strlen(obj.name) + 1];
this->age = obj.age;
strcpy(this->name, obj.name);
}
/*
~teacher()
{
if (this->age != NULL)
{
delete[]this->name;
}
this->age = 0;
this->name = NULL;
}
teacher* operator = (teacher&obj)
{
teacher* p;
p = &obj;
return p;
}*/
bool operator <(const teacher obj5)
{
return this->age < obj5.age;
}
};
bool chang(teacher obj1, teacher obj2)
{
cout << obj1.name << endl;
if (obj1.age < obj2.age)
{
return true;
}
else
{
return false;
}
}
void main02()
{
teacher ouyangdian("ouyangdian", 18);
teacher wuhui("wuhui", 25);
teacher wudong("wudong", 19);
teacher zhuwenjun("zhuwenjun", 17);
vector<teacher> array;
array.push_back(ouyangdian);
array.push_back(wuhui);
array.push_back(wudong);
array.push_back(zhuwenjun);
vector<teacher>::iterator it = array.begin();
sort(it, array.end(), chang);
vector<teacher>::iterator it1 = array.begin();
for (it1; it1 != array.end(); it1++)
{
cout << it1->name << " " << it1->age << endl;
}
}
void main()
{
//main03();
main02();
system("pause");
return;
}