How to compare C++ string using qsort in c?

前端 未结 6 554
时光说笑
时光说笑 2020-12-21 08:49

I tried to learn the qsort function of the c-library stdlib. This is provided even in c++. But i dont understand how to use them for sorting

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-21 09:07

    Works for me:

    #include
    #include
    using namespace std;
    #include
    
    int compare_str( const void *a, const void *b){
       string* obj = (string*)a;
       string* obj1 = (string*)b;
       return obj->compare(*obj1);
    }
    int main(){
        string obj[4] = {"fine", "ppoq", "tri", "get"};
        qsort(obj, 4, sizeof(string), compare_str);
        for( int i=0; i<4; i++)
            cout<

提交回复
热议问题