Hey Guys
I got this :
Qt Code:To copy to clipboard, switch view to plain text mode
now I want to know what is the fastest way to sort the list by wordItem.count ?!
Is there any function in Qt or C++ to do this for me ?!
Thank you for your time![]()
Hey Guys
I got this :
Qt Code:To copy to clipboard, switch view to plain text mode
now I want to know what is the fastest way to sort the list by wordItem.count ?!
Is there any function in Qt or C++ to do this for me ?!
Thank you for your time![]()
Last edited by gbmtoday; 2nd January 2011 at 20:38.
You can use qSort
gbmtoday (2nd January 2011)
Here's my code
Qt Code:
Qlist <WordItem> list; qStableSort(list.begin(),list.end(),compareWordItem); bool compareWordItem ( const WordItem &w1 , const WordItem &w2 ) { return w1.count > w2.count; }To copy to clipboard, switch view to plain text mode
but I get this :
error: no matching function for call to ‘qStableSort(QList<WordItem>::iterator, QList<WordItem>::iterator, <unresolved overloaded function type>)’
Did you #include <QtAlgorithms>?
Did you create a function prototype of the sort function before using it?
gbmtoday (2nd January 2011)
Also the compare function/functor should be declared before calling qSort
Qt Code:
bool compareWordItem ( const WordItem &w1 , const WordItem &w2 ) { return w1.count > w2.count; } qStableSort(list.begin(),list.end(),compareWordItem);To copy to clipboard, switch view to plain text mode
LE: you declare/define compareWordItem outside of any function (including main)
gbmtoday (2nd January 2011)
As Zlatomir says
This is your clue:
Qt Code:
<unresolved overloaded function type>To copy to clipboard, switch view to plain text mode
No, you should define that in the .cpp file, don't define functions in the .h file, you might get multiple-definitions errors if you include the header in many .cpp files.
In the header files you should put only declarations, not definitions...
I made a simple example, the code works, you should play with where you put the declarations/definitions, this is the little test i made:
Qt Code:
#include <QtCore> #include <iostream> struct WordItem { WordItem(int i) : count(i) {} int count; }; bool compareWordItem ( const WordItem &w1 , const WordItem &w2 ) { return w1.count > w2.count; } int main(int argc, char *argv[]) { QList<WordItem> list; list.append(10); list.append(11); list.append(1); qStableSort(list.begin(),list.end(),compareWordItem); for(QList<WordItem>::iterator i = list.begin(); i!= list.end(); ++i) qDebug() << (*i).count; std::cin.get(); return 0; }To copy to clipboard, switch view to plain text mode
gbmtoday (2nd January 2011)
No I meant I declared it in my .h and defined it .cpp ( sorry ! bad English )
I don't get it , my code looks exactly as yours except that mine is in a class .
finally I figured what is the problem .
I should have defined it as a static function.
in .h :
Qt Code:
static bool compareWordItem ( const WordItem &w1 , const WordItem &w2 );To copy to clipboard, switch view to plain text mode
thanks guys
Bookmarks