std::sort:Tell me what you want!

std::sort的第三个参数是一个函数指针,这个函数的原型是两个元素作比较,返回一个bool.我当时以为这个函数是这样设计的:函数是用来判断两个元素是否需要switch的,那么返回true就是需要switch,返回false就不用switch.

但实际情况好像不是.
那也许这个函数期望被设计成这样:

bool func(Type t1, Type t2){
  retrun what_you_want;
}

what you want,就是说你希望 t1>t2,那就 :

return t1>t2;

希望t1<t2,就:

 return t1<t2; 

返回你期望的情况!
ok,talk is cheap, let me show you the code:

#include <vector>
#include <algorithm>
#include <iostream>
struct PP{
    std::string name;
    std::string value;
};
std::ostream& operator<<(std::ostream& cout, PP const& p){
    return std::cout<<"name="<<p.name<<", value="<<p.value;
}

//我希望按照这样的顺序排序:在vector中把name属性为空的元素排到vector的末尾
bool WhatYouWant(PP const& p1, PP const& p2){
   //!(我不希望name属性为空的排在name不为空的前面) = what i want
    return !(p1.name == "" && p2.name !="");
}

void test3(){
    PP p1,p2,p3,p4,p5;
    p1.name = "haha";p1.value = ">-<";
    p2.name = "hehe";p2.value = ";P";
    p3.name = "";p3.value = ":)";
    p4.name = "nono";p4.value = ":(";
    p5.name = "gaga";p5.value = ":D";
    std::vector<PP> vp;
    vp.push_back(p1);
    vp.push_back(p2);
    vp.push_back(p3);
    vp.push_back(p4);
    vp.push_back(p5);
    std::cout<<"before:"<<std::endl;
    for(int i= 0;i<vp.size();i++){
        std::cout<<vp[i]<<std::endl;
    }
    //sort
    std::sort(vp.begin(),vp.end(), WhatYouWant);
    std::cout<<"after:"<<std::endl;
    for(int i= 0;i<vp.size();i++){
        std::cout<<vp[i]<<std::endl;
    }
}

int main(){
    test3();
    return 0;
}

run:


运行结果.png

虽然name为空的给排到后面去了,但好像...我不需要你给我搞乱其它顺序.
那我还是按自己的思路来吧:

template<typename T>
struct SwitchFunc{
    typedef bool (*type)(T const&, T const&);
};
template<typename T>
std::vector<T> BubbleSort(std::vector<T> const& vec, typename SwitchFunc<T>::type what_you_want){
    int size = vec.size();
    std::vector<T const*> pts;
    auto it = vec.begin();

    while(it!=vec.end()){
        pts.push_back( &(*it) );
        it++;
    }

    //bubble sort
    for(int i = 0;i < size - 1;i++){
        for(int j = 0; j + 1 < size - i; j++){
            if(!what_you_want(*pts[j], *pts[j+1])){
                T const* temp = pts[j];
                pts[j] = pts[j+1];
                pts[j+1] = temp;
            }
        }
    }
    
    std::vector<T> sorted;
    for(int i = 0;i < size ;i++){
        sorted.push_back(*pts[i]);
    }
    return sorted;
}

void test4(){
    PP p1,p2,p3,p4,p5;
    p1.name = "haha";p1.value = ">-<";
    p2.name = "hehe";p2.value = ";P";
    p3.name = "";p3.value = ":)";
    p4.name = "nono";p4.value = ":(";
    p5.name = "gaga";p5.value = ":D";
    std::vector<PP> vp;
    vp.push_back(p1);
    vp.push_back(p2);
    vp.push_back(p3);
    vp.push_back(p4);
    vp.push_back(p5);
    std::cout<<"before:"<<std::endl;
    for(int i= 0;i<vp.size();i++){
        std::cout<<vp[i]<<std::endl;
    }
    //sort
    vp = BubbleSort(vp, WhatYouWant);
    std::cout<<"after:"<<std::endl;
    for(int i= 0;i<vp.size();i++){
        std::cout<<vp[i]<<std::endl;
    }
}
int main(){
    test4();
    return 0;
}

run:
冒泡.png

总之:这也算一种思维方式吧.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 今天感恩节哎,感谢一直在我身边的亲朋好友。感恩相遇!感恩不离不弃。 中午开了第一次的党会,身份的转变要...
    迷月闪星情阅读 10,617评论 0 11
  • 彩排完,天已黑
    刘凯书法阅读 4,281评论 1 3
  • 表情是什么,我认为表情就是表现出来的情绪。表情可以传达很多信息。高兴了当然就笑了,难过就哭了。两者是相互影响密不可...
    Persistenc_6aea阅读 126,151评论 2 7