2

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.

#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;
}
user1653150
  • 353
  • 1
  • 3
  • 15

5 Answers5

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(text2.begin(), text2.end(),
    [](const char* lhs, const char* rhs) { return strcmp(lhs,rhs) < 0; });

That's one problem, there may be others.

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
2

One problem is in your compare function for 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:

int cst_cmp(const void *one, const void *two) 
{ 
return (strcmp(*(char **)one, *(char **)two));
}
P.P
  • 117,907
  • 20
  • 175
  • 238
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.

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

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

Massimiliano
  • 7,842
  • 2
  • 47
  • 62
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.

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!).

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • hi so std::to_string will convert the int to string, is it a c-style sting? – user1653150 Sep 22 '12 at 17:31
  • No, it's a C++ string. You don't want to return a C style string, because it just doesn't work! – Bo Persson Sep 22 '12 at 17:33
  • but what if I want a c-style sting what I would have to change, then – user1653150 Sep 22 '12 at 17:55
  • You would have to allocate space for the string using `malloc` (not forgetting an extra byte for the string terminator), create a copy using `strcpy`, return a pointer to that, and not forget to call `free` on that pointer later (and exactly once). Or use a `std::string` which does everything automatically. – Bo Persson Sep 22 '12 at 18:02
0

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