djadmin
djadmin

Reputation: 1760

How to use qsort for string in C++

I want to use qsort function to sort the characters in the strings using C++.

#include<iostream>
#include<string>
#include<cstdlib>

using namespace std;

int compare_str(void const *a,void const *b){
    char const *aa=(char const *)a;
    char const *bb=(char const *)b;

    if(*aa==*bb) return 0;
    else if(*aa>*bb) return 1;
    else return -1;
}
int main(){

    string str="cake";
    int len=str.length();

    qsort(str,len,sizeof(str[0]),compare_str);
    cout<<str;
    return 0;
}

But it throws :

20 42 [Error] cannot convert 'std::string {aka std::basic_string<char>}' to 'void*' for argument '1' to 'void qsort(void*, size_t, size_t, int (*)(const void*, const void*))' 

It would be great if anyone could provide an efficient way to do this.

Upvotes: 5

Views: 5593

Answers (5)

BlackMamba
BlackMamba

Reputation: 10252

#include<iostream>
#include<string>
#include<cstdlib>

using namespace std;

int compare_str(void const *a,void const *b){
    char const *aa=(char const *)a;
    char const *bb=(char const *)b;

    if(*aa==*bb) return 0;
    else if(*aa>*bb) return 1;
    else return -1;
}
int main(){

    string str="cake";
    int len=str.length();

    qsort(const_cast<char*>(str.c_str()),len,sizeof(str[0]),compare_str);
    cout<<str<<endl;
    return 0;
}

Upvotes: 0

lukai
lukai

Reputation: 576

You should use the function sort() under the header <algorithm>. This function is very flexible and you can use it in different manner. For sorting as you wish in question you can just write:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int main()
{
string s="cake";

sort(s.begin(), s.end());

cout << s << endl;

return 0;
}

//output: acek

again by using sort() we can implement it in a range. If you want to sort first two element , the code will be

sort(s.begin(), s.begin()+2);

for above code the output will be

//output: acke

so if we want to sort first n element then we can write

sort(s.begin,s.begin()+n);

we can also modify the sort function. In that case we have to pass three parameter instead of two. The third parameter will be a functions which returns a bool value.For example , if we want to sort in descending order then our code will be like this

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

bool desc(char i, char j)
{
    return i>j;
}

int main()
{
    string s="cake";

    sort(s.begin(), s.end(),desc);

    cout << s << endl;

    return 0;
}

//output: keca

Upvotes: 0

SeaBass
SeaBass

Reputation: 121

If you really want to do this, just pass a pointer to the string's contents:

qsort(str.c_str(),len,sizeof(str[0]),compare_str);

That said, you really should consider using the functions provided in the STL rather than those from the old C library...

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727137

Your comparator for qsort expects C strings, not C++ std::strings. You should either declare str to be char str[]

char str[] = "cake";
qsort(str, strlen(cake), sizeof(char), compare_str); // Consider renaming to compare_char

or (better) use std::sort:

string str = "cake";
sort(str.begin(), str.end());

Upvotes: 4

user2530166
user2530166

Reputation:

I strongly recommend the modern method of

#include <algorithm>
#include <iostream>
#include <string>

int main()
{
    std::string s("cake");

    std::sort(s.begin(), s.end());

    std::cout << s << std::endl; // Prints "acek".

    return 0;
}

Plus, using std::sort over qsort allows the compiler to optimize better, so it's a win-win...

Upvotes: 15

Related Questions