Results 1 to 5 of 5

Thread: Compare QString alphabet position

  1. #1
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Compare QString alphabet position

    compare qstring...

    I search a similar QString function from php strcmp
    http://php.net/strcmp to find the nummer on 2° position from a qmap...

    How make this...

    Example ... if your name is "zumalli" the formula take only the first 3 lovercase letter
    "zum" and i musst return 993

    if Your name is "zcello" take zce and from compare alphabet position i must return 979


    Qt Code:
    1. typedef QMap<int, QStringList> TableAi;
    2. TableAi Box_att;
    3. Box_att.clear();
    4. ................................................
    5. Box_att.insert(875,QStringList() << "x" << "975");
    6. Box_att.insert(876,QStringList() << "z" << "976");
    7. Box_att.insert(877,QStringList() << "zam" << "977");
    8. Box_att.insert(878,QStringList() << "zau" << "978");
    9. Box_att.insert(879,QStringList() << "zb" << "979");
    10. Box_att.insert(880,QStringList() << "ze" << "980");
    11. Box_att.insert(881,QStringList() << "zeh" << "981");
    12. Box_att.insert(882,QStringList() << "zei" << "982");
    13. Box_att.insert(883,QStringList() << "zem" << "983");
    14. Box_att.insert(884,QStringList() << "zf" << "984");
    15. Box_att.insert(885,QStringList() << "zi" << "985");
    16. Box_att.insert(886,QStringList() << "zim" << "986");
    17. Box_att.insert(887,QStringList() << "zin" << "987");
    18. Box_att.insert(888,QStringList() << "zk" << "988");
    19. Box_att.insert(889,QStringList() << "zo" << "989");
    20. Box_att.insert(890,QStringList() << "zu" << "990");
    21. Box_att.insert(891,QStringList() << "zuc" << "991");
    22. Box_att.insert(892,QStringList() << "zul" << "992");
    23. Box_att.insert(893,QStringList() << "zum" << "993");
    24. Box_att.insert(894,QStringList() << "zun" << "994");
    25. Box_att.insert(895,QStringList() << "zur" << "995");
    26. Box_att.insert(896,QStringList() << "zus" << "996");
    27. Box_att.insert(897,QStringList() << "zw" << "997");
    28. Box_att.insert(898,QStringList() << "zwe" << "998");
    29. Box_att.insert(899,QStringList() << "zy" << "999");
    30.  
    31.  
    32. /* incomming form value nam */
    33. QString firsttree="000";
    34. QString nametree="zce"; /* must become alphabet 979 */
    35.  
    36. TableAi::Iterator it;
    37. for ( it = Box_att.begin(); it != Box_att.end(); ++it ) {
    38. QStringList itemsetter = it.value();
    39. QString letter = QString(itemsetter.at(0));
    40. QString nummer = QString(itemsetter.at(1));
    41. if (nametree > letter) { /* compare alphabet position */
    42. return nummer;
    43. }
    44. }
    45. /*
    46.   original from php
    47.   $fp = fopen ("ahvkeys.csv","r");
    48.   while ($data0 = fgetcsv ($fp, 1000, ",")) {
    49.   if(strcmp ($data0[0], $name)>0) break;
    50.   $data1=$data0;
    51.   }
    52.   $first3letternummer=$data1[1];
    53.  
    54.   */
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Compare QString alphabet position

    I solved....

    strcmp is a php function & a c++

    if (strcmp(ainame,yourname) > 0) { break; } register the qmap position

    and take position - 1 value ... thats it... tanksss

  3. #3
    Join Date
    Jan 2006
    Location
    Earth (Terra)
    Posts
    87
    Thanks
    4
    Thanked 6 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Compare QString alphabet position

    Just a few notes on operators that you might find useful. From your original code:

    Qt Code:
    1. TableAi::Iterator it;
    2. for ( it = Box_att.begin(); it != Box_att.end(); ++it )
    3. {
    4. QStringList itemsetter = it.value();
    5. QString letter = QString(itemsetter.at(0));
    6. QString nummer = QString(itemsetter.at(1));
    7. if (nametree > letter)
    8. {
    9. /* compare alphabet position */
    10. return nummer;
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 
    You should be able to do this:
    Qt Code:
    1. TableAi::Iterator it;
    2. for ( it = Box_att.begin(); it != Box_att.end(); ++it )
    3. {
    4. QString letter = it[0];
    5. QString nummer = it[1]);
    6. if (nametree > letter)
    7. {
    8. /* compare alphabet position */
    9. return nummer;
    10. }
    11.  
    12. return QString::null; // have to return something if fallthrough
    To copy to clipboard, switch view to plain text mode 

    Notes:
    • The iterator acts as an instance of the object in the array.
    • You don't need to wrap the itemsetter returns in a QString() wrapper - in fact, that's kind of wasteful, since it creates a copy that you don't need (although QString does optimize copies under the covers to point to the same string. Still, unless you're going to change the copy, it's a waste.)


    Taking it further:
    Qt Code:
    1. enum ItemIndexes { LETTER, NUMBER }
    2. // (do this somewhere)
    3. // might want to wrap in a namespace or class somewhere, or prefix
    4. // the items to guarantee uniqueness
    5.  
    6. TableAi::Iterator it;
    7. for ( it = Box_att.begin(); it != Box_att.end(); ++it )
    8. if (nametree > it[LETTER]) /* compare alphabet position */
    9. return it[NUMBER];
    10.  
    11. return QString::null; // Have to return something if fallthrough
    To copy to clipboard, switch view to plain text mode 

    In fact, I think if you're using Qt4, you can use the 'foreach' keyword on iterators.

    Consolidating the code makes the logic more apparent - in doing this, I discovered you weren't returning anything on fallthrough & added that.

    rickb
    Last edited by rickbsgu; 2nd July 2006 at 15:32.

  4. #4
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Compare QString alphabet position

    Your method not run on qt4....

    strcmp wand char .... a qt similar function not exist....


    Qt Code:
    1. TableAi::Iterator it;
    2. for ( it = Box_att.begin(); it != Box_att.end(); ++it ) {
    3. loop++; /* register the map position to take value one before break */
    4. if (strcmp(it[0],yourname) > 0) { break; }
    5. }
    To copy to clipboard, switch view to plain text mode 



    gui_main.cpp:1069: error: cannot convert `QMapData::Node' to `const char*' for a
    rgument `1' to `int strcmp(const char*, const char*)'
    mingw32-make[1]: *** [build\.o\win32\gui_main.o] Error 1
    mingw32-make[1]: Leaving directory `C:/_0000_current/src_datum'

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Compare QString alphabet position

    Quote Originally Posted by patrik08
    strcmp wand char .... a qt similar function not exist....
    On the contrary, it does exist: http://doc.trolltech.com/4.1/qstring.html#compare

Similar Threads

  1. QSqlQueryModel + set Write
    By raphaelf in forum Qt Programming
    Replies: 7
    Last Post: 5th June 2006, 08:55
  2. Converting QString to unsigned char
    By salston in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 22:10
  3. dialog box
    By Bahar in forum Qt Programming
    Replies: 3
    Last Post: 31st January 2006, 14:52
  4. [SOLVED] Widget plugin ... how to ?
    By yellowmat in forum Newbie
    Replies: 10
    Last Post: 29th January 2006, 20:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.