std sort使用

#include <stdio.h>
#include <list>
#include <vector>
#include <algorithm>
 
using namespace std;
 
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; 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值