foreach - basic C++ question
-
EDITED 1. I thought 'foreach" was a C++ "feature" - in QT doc they call it macro "Q_FOREACH" - not sure which way is up... 2. The syntax foreach(QString str, StringArray[MAX_ARRAY]) is the one which "assigns" INDIVIDUAL char to str I need the entire string of specific array , not the char Irregardless if it is a macro or not what is correct syntax to get the entire string USING "foreach" ? From the description - it is an iterator and the object of iteration is vaguely described - sort of "automatic". . If I iterate QString and expect QString - one usage gives me full string - from start to last new line and another gives me INDIVIDUAL characters- one by one. What am I doing different ?
-
EDITED 1. I thought 'foreach" was a C++ "feature" - in QT doc they call it macro "Q_FOREACH" - not sure which way is up... 2. The syntax foreach(QString str, StringArray[MAX_ARRAY]) is the one which "assigns" INDIVIDUAL char to str I need the entire string of specific array , not the char Irregardless if it is a macro or not what is correct syntax to get the entire string USING "foreach" ? From the description - it is an iterator and the object of iteration is vaguely described - sort of "automatic". . If I iterate QString and expect QString - one usage gives me full string - from start to last new line and another gives me INDIVIDUAL characters- one by one. What am I doing different ?
Did you try to compare your these two "usages" to see the difference between them?
-
EDITED 1. I thought 'foreach" was a C++ "feature" - in QT doc they call it macro "Q_FOREACH" - not sure which way is up... 2. The syntax foreach(QString str, StringArray[MAX_ARRAY]) is the one which "assigns" INDIVIDUAL char to str I need the entire string of specific array , not the char Irregardless if it is a macro or not what is correct syntax to get the entire string USING "foreach" ? From the description - it is an iterator and the object of iteration is vaguely described - sort of "automatic". . If I iterate QString and expect QString - one usage gives me full string - from start to last new line and another gives me INDIVIDUAL characters- one by one. What am I doing different ?
I'm no QT expert, so YMMV. You could probably just iterate the QString as an array? see : [https://stackoverflow.com/questions/18916099/search-qstring-character-by-character\](https://stackoverflow.com/questions/18916099/search-qstring-character-by-character)
CI/CD = Continuous Impediment/Continuous Despair
-
EDITED 1. I thought 'foreach" was a C++ "feature" - in QT doc they call it macro "Q_FOREACH" - not sure which way is up... 2. The syntax foreach(QString str, StringArray[MAX_ARRAY]) is the one which "assigns" INDIVIDUAL char to str I need the entire string of specific array , not the char Irregardless if it is a macro or not what is correct syntax to get the entire string USING "foreach" ? From the description - it is an iterator and the object of iteration is vaguely described - sort of "automatic". . If I iterate QString and expect QString - one usage gives me full string - from start to last new line and another gives me INDIVIDUAL characters- one by one. What am I doing different ?
Quote:
1. I thought 'foreach" was a C++ "feature" - in QT doc they call it macro "Q_FOREACH" - not sure which way is up...
Then you were wrong.
C++
has NOforeach
construct, however, it provides the range-basedfor
, see Range-based for loop (since C++11) - cppreference.com[^].Quote:
2. The syntax foreach(QString str, StringArray[MAX_ARRAY])
I cannot find the
StringArray
class inQT
documentation. Do you meant QStringList Class | Qt Core 6.3.2[^]? On the other hand, if you use a (C
-like) array ofQString
s, then you could use theC++
range-basedfor
."In testa che avete, Signor di Ceprano?" -- Rigoletto
-
EDITED 1. I thought 'foreach" was a C++ "feature" - in QT doc they call it macro "Q_FOREACH" - not sure which way is up... 2. The syntax foreach(QString str, StringArray[MAX_ARRAY]) is the one which "assigns" INDIVIDUAL char to str I need the entire string of specific array , not the char Irregardless if it is a macro or not what is correct syntax to get the entire string USING "foreach" ? From the description - it is an iterator and the object of iteration is vaguely described - sort of "automatic". . If I iterate QString and expect QString - one usage gives me full string - from start to last new line and another gives me INDIVIDUAL characters- one by one. What am I doing different ?
QString
offers a standard C++ iterator: QString Class | Qt Core 6.3.2[^]. So (assumingqstr
is the reference to your string) you can try something like:for (QString::iterator it = qstr->begin(); it != qstr->end(); ++it)
{
// it now points to each character in turn
} -
Quote:
1. I thought 'foreach" was a C++ "feature" - in QT doc they call it macro "Q_FOREACH" - not sure which way is up...
Then you were wrong.
C++
has NOforeach
construct, however, it provides the range-basedfor
, see Range-based for loop (since C++11) - cppreference.com[^].Quote:
2. The syntax foreach(QString str, StringArray[MAX_ARRAY])
I cannot find the
StringArray
class inQT
documentation. Do you meant QStringList Class | Qt Core 6.3.2[^]? On the other hand, if you use a (C
-like) array ofQString
s, then you could use theC++
range-basedfor
."In testa che avete, Signor di Ceprano?" -- Rigoletto
Thanks, after few RTFM I finally came to realize it is QT macro. If I knew that such I should have study the macro and realize I was expecting wrong result. After all this I think it has , the QT macro, its place , but it essentially is a replacement for standard C "for loop ". Lesson learn.
-
Thanks, after few RTFM I finally came to realize it is QT macro. If I knew that such I should have study the macro and realize I was expecting wrong result. After all this I think it has , the QT macro, its place , but it essentially is a replacement for standard C "for loop ". Lesson learn.
-
Quote:
1. I thought 'foreach" was a C++ "feature" - in QT doc they call it macro "Q_FOREACH" - not sure which way is up...
Then you were wrong.
C++
has NOforeach
construct, however, it provides the range-basedfor
, see Range-based for loop (since C++11) - cppreference.com[^].Quote:
2. The syntax foreach(QString str, StringArray[MAX_ARRAY])
I cannot find the
StringArray
class inQT
documentation. Do you meant QStringList Class | Qt Core 6.3.2[^]? On the other hand, if you use a (C
-like) array ofQString
s, then you could use theC++
range-basedfor
."In testa che avete, Signor di Ceprano?" -- Rigoletto
It looks I was totally of. foreach(CommandString,BT_Setup[CommandIndex] ) works as expected since the BT_Setup is QStringList . It does what it is designed and with QString pulls separate characters. So the moral of the story - convert QString to QSTringList...