网上江湖郎中和蒙古大夫很多,因此,此类帖子也很多。关于排序,我还真没研究过,看了江湖郎中和蒙古大夫的帖子,搞了半天不行,所以,自己研究了一 下,如下:三种方式都可以,如重写<,()和写比较函数compare_index。但是要注意对象和对象指针的排序区别。
容器中是对象时,用<排序。
容器中是对象指针时,用()和比较函数排序都可以。
list用成员方法sort
vector用sort函数
- class TestIndex{
- public:
- int index;
- TestIndex(){
- }
- TestIndex(int _index):index(_index){
- }
- bool operator()(const TestIndex* t1,const TestIndex* t2){
- printf("Operator():%d,%d/n",t1->index,t2->index);
- return t1->index < t2->index;
- }
- bool operator < (const TestIndex& ti) const {
- printf("Operator<:%d/n",ti.index);
- return index < ti.index;
- }
- };
- bool compare_index(const TestIndex* t1,const TestIndex* t2){
- printf("CompareIndex:%d,%d/n",t1->index,t2->index);
- return t1->index < t2->index;
- }
- int main(int argc, char** argv) {
- list<TestIndex*> tiList1;
- list<TestIndex> tiList2;
- vector<TestIndex*> tiVec1;
- vector<TestIndex> tiVec2;
- TestIndex* t1 = new TestIndex(2);
- TestIndex* t2 = new TestIndex(1);
- TestIndex* t3 = new TestIndex(3);
- tiList1.push_back(t1);
- tiList1.push_back(t2);
- tiList1.push_back(t3);
- tiList2.push_back(*t1);
- tiList2.push_back(*t2);
- tiList2.push_back(*t3);
- tiVec1.push_back(t1);
- tiVec1.push_back(t2);
- tiVec1.push_back(t3);
- tiVec2.push_back(*t1);
- tiVec2.push_back(*t2);
- tiVec2.push_back(*t3);
- printf("tiList1.sort()/n");
- tiList1.sort();//无法正确排序
- printf("tiList2.sort()/n");
- tiList2.sort();//用<比较
- printf("tiList1.sort(TestIndex())/n");
- tiList1.sort(TestIndex());//用()比较
- printf("sort(tiVec1.begin(),tiVec1.end())/n");
- sort(tiVec1.begin(),tiVec1.end());//无法正确排序
- printf("sort(tiVec2.begin(),tiVec2.end())/n");
- sort(tiVec2.begin(),tiVec2.end());//用<比较
- printf("sort(tiVec1.begin(),tiVec1.end(),TestIndex())/n");
- sort(tiVec1.begin(),tiVec1.end(),TestIndex());//用()比较
- printf("sort(tiVec1.begin(),tiVec1.end(),compare_index)/n");
- sort(tiVec1.begin(),tiVec1.end(),compare_index);//用compare_index比较
- return 0;
- }
转自:http://blog.csdn.net/marising/article/details/4567531
其他:http://www.51testing.com/?uid-292219-action-viewspace-itemid-243476
是使用STL的sort排序自己的类,其实就是传入sort的第三个参数,具体由三种方法,分别是:
1.定义普通函数
2.定义函数对象
3.重载 "<"
代码如下:
定义排序函数:
bool Less(const Student& s1, const Student& s2)
{
return s1.name < s2.name; //可以自己设定
}
std::sort(sutVector.begin(), stuVector.end(), Less);
重载 "<"
bool operator<(const Student& s1, const Student& s2)
{
return s1.name < s2.name; //可以自己设定
}
std::sort(sutVector.begin(), stuVector.end());
定义函数对象
struct Less
{
bool operator()(const Student& s1, const Student& s2)
{
return s1.name < s2.name; //可以自己设定
}
};
std::sort(sutVector.begin(), stuVector.end(), Less());
=======================================================================================
下面是个完整的Demo:已编译通过
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
class Student;
ostream& operator<<(ostream& os,const Student& _Student);
bool operator<(const Student& s1, const Student& s2);
// 由于编译器本身的问题,重载运算符的函数做友元的时候要先声明一下
// (此说法来自互联网),没有权威的说明,但是确实的这样,你可以看到
// test函数和myless类都没有任何问题
class Student {
private:
char name[32];
int goal;
public:
Student(char *_name, int _goal)
{
strcpy(name, _name);
goal = _goal;
}
// 友元函数声明
friend bool operator<(const Student& s1, const Student& s2);
friend ostream& operator<<(ostream& os,const Student& _Student);
friend class myless;
friend bool test(const Student& s1);
};
bool test(const Student& s1)
{
cout<< s1.goal << endl;
return true;
}
// 重载"<"实现泛型算法sort
bool operator<(const Student& s1, const Student& s2)
{
return s1.goal < s2.goal;
}
// 重载输出流迭代子
ostream& operator<<(ostream& os,const Student& _Student)
{
os<<_Student.name<<" "<<_Student.goal;
return os;
}
// 函数对象,用于升序排列
class myless
{
public:
bool operator()(const Student& s1, const Student& s2)
{
return s1.goal > s2.goal;
}
};
int main()
{
vector<Student> col1;
ostream_iterator< Student > output( cout, "\n" );
Student A1("xiao1", 99);
Student A2("xiao2", 23);
Student A3("xiao3", 53);
col1.push_back(A1);
col1.push_back(A2);
col1.push_back(A3);
cout << "Vector col1 contains: \n";
copy( col1.begin(), col1.end(), output ); // 输出初始列表容器col1中的元素
sort(col1.begin(),col1.end()); // 第一种调用形式
cout << "\nAfter sorted in ascending order col1 contains: \n";
copy( col1.begin(), col1.end(), output ); // 升序排序元素后列表容器col1中的元素
sort(col1.begin(),col1.end(),myless()); // 第二种调用形式使用标准函数对象
cout << "\nAfter sorted in descending ordercol1 contains: \n";
copy( col1.begin(), col1.end(), output ); // 降序排序元素后列表容器col1中的元素
cout<<endl;
col1.clear();
return 0;
}