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
Code:
typedef QMap<int, QStringList> TableAi;
TableAi Box_att;
Box_att.clear();
................................................
/* incomming form value nam */
QString nametree
="zce";
/* must become alphabet 979 */
TableAi::Iterator it;
for ( it = Box_att.begin(); it != Box_att.end(); ++it ) {
if (nametree > letter) { /* compare alphabet position */
return nummer;
}
}
/*
original from php
$fp = fopen ("ahvkeys.csv","r");
while ($data0 = fgetcsv ($fp, 1000, ",")) {
if(strcmp ($data0[0], $name)>0) break;
$data1=$data0;
}
$first3letternummer=$data1[1];
*/
Re: Compare QString alphabet position
I solved....
strcmp is a php function & a c++ :rolleyes:
if (strcmp(ainame,yourname) > 0) { break; } register the qmap position
and take position - 1 value ... thats it... tanksss
Re: Compare QString alphabet position
Just a few notes on operators that you might find useful. From your original code:
Quote:
Code:
TableAi::Iterator it;
for ( it = Box_att.begin(); it != Box_att.end(); ++it )
{
if (nametree > letter)
{
/* compare alphabet position */
return nummer;
}
}
You should be able to do this:
Code:
TableAi::Iterator it;
for ( it = Box_att.begin(); it != Box_att.end(); ++it )
{
if (nametree > letter)
{
/* compare alphabet position */
return nummer;
}
return QString::null;
// have to return something if fallthrough
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:
Code:
enum ItemIndexes { LETTER, NUMBER }
// (do this somewhere)
// might want to wrap in a namespace or class somewhere, or prefix
// the items to guarantee uniqueness
TableAi::Iterator it;
for ( it = Box_att.begin(); it != Box_att.end(); ++it )
if (nametree > it[LETTER]) /* compare alphabet position */
return it[NUMBER];
return QString::null;
// Have to return something if fallthrough
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
Re: Compare QString alphabet position
Your method not run on qt4....
strcmp wand char .... a qt similar function not exist....
Code:
TableAi::Iterator it;
for ( it = Box_att.begin(); it != Box_att.end(); ++it ) {
loop++; /* register the map position to take value one before break */
if (strcmp(it[0],yourname) > 0) { break; }
}
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'
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