我正在尝试同时使用 sort 和 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;
}