简体   繁体   English

使用sort和qsort进行c样式字符串排序

[英]c-style string sorting with sort and qsort

I'm trying to use both sort and qsort to sort a c-style string and them see which of them is better, so I've written this code, but it is not working , so can you please tell me what is wrong with it. 我正在尝试使用sort和qsort来排序一个c风格的字符串,他们看到哪个更好,所以我写了这个代码,但它没有用,所以你能告诉我有什么问题吗?它。 thanks in advance. 提前致谢。

#include <iostream>
#include<vector>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<chrono>
#include<string>
#include<sstream>

using namespace std;
using namespace std::chrono;

void bvect(vector<double> &vec, int num)
{
     auto gen = bind(normal_distribution<double>(15,4.0),default_random_engine());
     for(int i=0; i<num; ++i)
             vec.push_back(gen());
}

char* converttostring(int number)
{
   stringstream ss;
   ss << number;
   return (ss.c_str());
}

int cst_cmp(const void *one, const void *two) 
{ 
     char a = *((char*)one);
     char b = *((char*)two);
    return strcmp(a, b);
}

//Generated random strings
void textvect(vector<string> &vec, int num)
{
   srand(time(NULL));
     for(int i=0; i<num; ++i) 
             vec.push_back(converttostring(rand()%num +1));
}


void displayvector(vector<char*>vect)
{
     for (int i=0; i<vect.size(); ++i){
         for (int j=0; j<strlen(vect[i]); ++j)
         cout<<vect[i][j];
         cout<<endl;
         }
}

int main(){
    int sz=100000;
    vector<char*>text1, text2;
    textvect(text1, sz);
    text2.resize(text1.size());
    copy(text1.begin(),text1.end(),text2.begin());

    // qsort() string
    auto t1 = system_clock::now();
    qsort(&text1[0], text1.size(), sizeof(char*), cst_cmp);
    auto t2 = system_clock::now();
    auto dms = duration_cast<milliseconds>(t2-t1);
    cout << "string qsort() took " << dms.count() << " milliseconds\n";

    // sort() string
    auto t3 = system_clock::now();  
    std::sort(text2.begin(), text2.end());
    auto t4 = system_clock::now();
    auto dms1 = duration_cast<milliseconds>(t4-t3);
    cout << "string sort() took " << dms1.count() << " milliseconds\n";

    return 0;
}

For std::sort , you are just using the default comparator, which will just compare pointer values. 对于std::sort ,您只使用默认比较器,它只比较指针值。 You need to pass a comparator that does a proper comparison (using strcmp, for example): 您需要传递一个比较器,进行适当的比较(例如,使用strcmp):

std::sort(text2.begin(), text2.end(),
    [](const char* lhs, const char* rhs) { return strcmp(lhs,rhs) < 0; });

That's one problem, there may be others. 这是一个问题,可能还有其他问题。

One problem is in your compare function for qsort : 一个问题是qsort的比较函数:

int cst_cmp(const void *one, const void *two) 
{ 
     char a = *((char*)one);
     char b = *((char*)two);
    return strcmp(a, b);
}

You are not comparing strings here, because a and b are just chars. 你不是在这里比较字符串,因为ab只是字符。 You might as well avoid them: 你也可以避免它们:

int cst_cmp(const void *one, const void *two) 
{ 
return (strcmp(*(char **)one, *(char **)two));
}

These are the errors I obtain trying to compile your code: 这些是我尝试编译代码时遇到的错误:

> g++ main.cc -std=c++0x
main.cc: In function ‘char* converttostring(int)’:
main.cc:24:15: error: ‘std::stringstream’ has no member named ‘c_str’
main.cc: In function ‘int cst_cmp(const void*, const void*)’:
main.cc:31:23: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
/usr/include/string.h:143:12: error:   initializing argument 1 of ‘int strcmp(const char*, const char*)’ [-fpermissive]
main.cc:31:23: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
/usr/include/string.h:143:12: error:   initializing argument 2 of ‘int strcmp(const char*, const char*)’ [-fpermissive]
main.cc: In function ‘int main()’:
main.cc:55:23: error: invalid initialization of reference of type ‘std::vector<std::basic_string<char> >&’ from expression of type ‘std::vector<char*>’
main.cc:35:6: error: in passing argument 1 of ‘void textvect(std::vector<std::basic_string<char> >&, int)’

24:15 c_str() is a member function of string not of stringstream . 24:15 c_str()string不是stringstream的成员函数。 See here . 看到这里

31:23 strcmp() wants two const char * not two char . 31:23 strcmp()想要两个const char *而不是两个char See here . 看到这里

55:23 and 35:6 char* is not the same type as string . 55:23和35:6 char*string类型不同。

This function isn't working 此功能无效

char* converttostring(int number)
{
   stringstream ss;
   ss << number;
   return (ss.c_str());
}

and if it was sort of fixed ( ss.str().c_str() ), it would return a pointer to a temporary. 如果它是一种固定的( ss.str().c_str() ),它将返回一个指向临时的指针。

If you have a compiler with some C++11 support, you can use std::to_string from the standard library. 如果您的编译器支持某些C ++ 11,则可以使用标准库中的std::to_string Otherwise, change the return type to std::string (no pointer!). 否则,将返回类型更改为std::string (无指针!)。

问Stroustrup;)只是为C字符串数组分配空间并输入字符ino it ..记得解除分配它..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM