Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. foreach - basic C++ question

foreach - basic C++ question

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++data-structures
8 Posts 4 Posters 12 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    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 ?

    V M CPalliniC L 4 Replies Last reply
    0
    • L Lost User

      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 ?

      V Offline
      V Offline
      Victor Nijegorodov
      wrote on last edited by
      #2

      Did you try to compare your these two "usages" to see the difference between them?

      1 Reply Last reply
      0
      • L Lost User

        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 ?

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • L Lost User

          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 ?

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          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 NO foreach construct, however, it provides the range-based for, 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 in QT documentation. Do you meant QStringList Class | Qt Core 6.3.2[^]? On the other hand, if you use a (C-like) array of QStrings, then you could use the C++ range-based for.

          "In testa che avete, Signor di Ceprano?" -- Rigoletto

          In testa che avete, signor di Ceprano?

          L 2 Replies Last reply
          0
          • L Lost User

            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 ?

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            QString offers a standard C++ iterator: QString Class | Qt Core 6.3.2[^]. So (assuming qstr 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
            }

            1 Reply Last reply
            0
            • CPalliniC CPallini

              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 NO foreach construct, however, it provides the range-based for, 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 in QT documentation. Do you meant QStringList Class | Qt Core 6.3.2[^]? On the other hand, if you use a (C-like) array of QStrings, then you could use the C++ range-based for.

              "In testa che avete, Signor di Ceprano?" -- Rigoletto

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              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.

              CPalliniC 1 Reply Last reply
              0
              • L Lost User

                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.

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #7

                You are welcome.

                "In testa che avete, Signor di Ceprano?" -- Rigoletto

                In testa che avete, signor di Ceprano?

                1 Reply Last reply
                0
                • CPalliniC CPallini

                  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 NO foreach construct, however, it provides the range-based for, 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 in QT documentation. Do you meant QStringList Class | Qt Core 6.3.2[^]? On the other hand, if you use a (C-like) array of QStrings, then you could use the C++ range-based for.

                  "In testa che avete, Signor di Ceprano?" -- Rigoletto

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  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...

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups