c样式的字符串排序,排序和排序。

时间:2021-07-14 22:28:00

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. thanks in advance.

我尝试使用排序和qsort来排序一个c样式的字符串,他们看到哪一个更好,所以我写了这个代码,但是它不工作,所以你能告诉我它有什么问题吗?提前谢谢。

#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;
}

5 个解决方案

#1


2  

For std::sort, you are just using the default comparator, which will just compare pointer values. You need to pass a comparator that does a proper comparison (using strcmp, for example):

对于std::sort,您只是使用默认的比较器,它只会比较指针值。您需要通过一个比较器来进行适当的比较(例如,使用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.

这是一个问题,可能还有其他问题。

#2


2  

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. You might as well avoid them:

你没有在这里比较字符串,因为a和b只是chars。你不妨避开它们:

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

#3


2  

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. See here.

24:15 c_str()是字符串的成员函数,而不是stringstream。在这里看到的。

31:23 strcmp() wants two const char * not two char. See here.

strcmp()需要两个const char *而不是两个char。在这里看到的。

55:23 and 35:6 char* is not the same type as string.

55:23和35:6 char*与字符串不相同。

#4


2  

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. Otherwise, change the return type to std::string (no pointer!).

如果您有一个具有一些c++ 11支持的编译器,您可以使用std::to_string从标准库中。否则,将返回类型更改为std::string(没有指针!)

#5


0  

Ask Stroustrup ;) just allocate space for the C string array and enter characters ino it.. remember to deallocate it..

请求Stroustrup;)为C字符串数组分配空间并输入字符ino。记得去释放它。

#1


2  

For std::sort, you are just using the default comparator, which will just compare pointer values. You need to pass a comparator that does a proper comparison (using strcmp, for example):

对于std::sort,您只是使用默认的比较器,它只会比较指针值。您需要通过一个比较器来进行适当的比较(例如,使用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.

这是一个问题,可能还有其他问题。

#2


2  

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. You might as well avoid them:

你没有在这里比较字符串,因为a和b只是chars。你不妨避开它们:

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

#3


2  

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. See here.

24:15 c_str()是字符串的成员函数,而不是stringstream。在这里看到的。

31:23 strcmp() wants two const char * not two char. See here.

strcmp()需要两个const char *而不是两个char。在这里看到的。

55:23 and 35:6 char* is not the same type as string.

55:23和35:6 char*与字符串不相同。

#4


2  

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. Otherwise, change the return type to std::string (no pointer!).

如果您有一个具有一些c++ 11支持的编译器,您可以使用std::to_string从标准库中。否则,将返回类型更改为std::string(没有指针!)

#5


0  

Ask Stroustrup ;) just allocate space for the C string array and enter characters ino it.. remember to deallocate it..

请求Stroustrup;)为C字符串数组分配空间并输入字符ino。记得去释放它。