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;
}
7
  • 2
    Can you be more specific than it's not working? What does it do? Crash? Not compile? The output is wrong?
    – Borgleader
    Commented Sep 22, 2012 at 16:45
  • 3
    Converttostring is returning a pointer to a local. That is not going to work. It should perhaps return string not char*. Commented Sep 22, 2012 at 16:51
  • std::generate_n(std::back_inserter(vec), num, gen);
    – obataku
    Commented Sep 22, 2012 at 16:53
  • 1
    Duplicate? stackoverflow.com/questions/12459290/…
    – PiotrNycz
    Commented Sep 22, 2012 at 17:25
  • hes passing a vector<char*> to a function expecting a vector<string>. This doesnt even come close to compiling. Commented Sep 22, 2012 at 17:38

5 Answers 5

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.

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));
}
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.

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

4
  • hi so std::to_string will convert the int to string, is it a c-style sting? Commented Sep 22, 2012 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
    Commented Sep 22, 2012 at 17:33
  • but what if I want a c-style sting what I would have to change, then Commented Sep 22, 2012 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
    Commented Sep 22, 2012 at 18:02
0

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

1
  • This should be a comment rather than an answer.
    – shadyyx
    Commented Sep 24, 2012 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.