Note that there are some explanatory texts on larger screens.

plurals
  1. POc-style string sorting with sort and qsort
    primarykey
    data
    text
    <p>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.</p> <pre><code>#include &lt;iostream&gt; #include&lt;vector&gt; #include&lt;cstdlib&gt; #include&lt;algorithm&gt; #include&lt;cstring&gt; #include&lt;chrono&gt; #include&lt;string&gt; #include&lt;sstream&gt; using namespace std; using namespace std::chrono; void bvect(vector&lt;double&gt; &amp;vec, int num) { auto gen = bind(normal_distribution&lt;double&gt;(15,4.0),default_random_engine()); for(int i=0; i&lt;num; ++i) vec.push_back(gen()); } char* converttostring(int number) { stringstream ss; ss &lt;&lt; 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&lt;string&gt; &amp;vec, int num) { srand(time(NULL)); for(int i=0; i&lt;num; ++i) vec.push_back(converttostring(rand()%num +1)); } void displayvector(vector&lt;char*&gt;vect) { for (int i=0; i&lt;vect.size(); ++i){ for (int j=0; j&lt;strlen(vect[i]); ++j) cout&lt;&lt;vect[i][j]; cout&lt;&lt;endl; } } int main(){ int sz=100000; vector&lt;char*&gt;text1, text2; textvect(text1, sz); text2.resize(text1.size()); copy(text1.begin(),text1.end(),text2.begin()); // qsort() string auto t1 = system_clock::now(); qsort(&amp;text1[0], text1.size(), sizeof(char*), cst_cmp); auto t2 = system_clock::now(); auto dms = duration_cast&lt;milliseconds&gt;(t2-t1); cout &lt;&lt; "string qsort() took " &lt;&lt; dms.count() &lt;&lt; " milliseconds\n"; // sort() string auto t3 = system_clock::now(); std::sort(text2.begin(), text2.end()); auto t4 = system_clock::now(); auto dms1 = duration_cast&lt;milliseconds&gt;(t4-t3); cout &lt;&lt; "string sort() took " &lt;&lt; dms1.count() &lt;&lt; " milliseconds\n"; return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload