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() ??

foreach() ??

Scheduled Pinned Locked Moved C / C++ / MFC
databasedesigntutorialquestionlounge
6 Posts 5 Posters 10 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

    I like to learn how to use "foreach". I have posted similar request on another forum and got "RTFM" Could somebody REWRiTE the attached using "foreach" ? ..and a simple explanation of code used ( called "comments" ) would be nice. I need to start with "foreach" before goto "new style" of "for". Thanks

    localAdapters = QBluetoothLocalDevice::allDevices();
    count = localAdapters.count();
    ui->chat_10->clear();

    qDebug()<< " localAdapters  count " << QString::number(count);
    for(int index = 0; index < count; index++)
    {
        qDebug()<< " local adapter address" << localAdapters.at(index).address().toString();
        ui->chat\_10->append( localAdapters.at(index).address().toString());
        qDebug() << " local adapater name " << localAdapters.at(index).name();
        ui->chat\_10->append( localAdapters.at(index).name());
    }
    
    Mircea NeacsuM CPalliniC U 3 Replies Last reply
    0
    • L Lost User

      I like to learn how to use "foreach". I have posted similar request on another forum and got "RTFM" Could somebody REWRiTE the attached using "foreach" ? ..and a simple explanation of code used ( called "comments" ) would be nice. I need to start with "foreach" before goto "new style" of "for". Thanks

      localAdapters = QBluetoothLocalDevice::allDevices();
      count = localAdapters.count();
      ui->chat_10->clear();

      qDebug()<< " localAdapters  count " << QString::number(count);
      for(int index = 0; index < count; index++)
      {
          qDebug()<< " local adapter address" << localAdapters.at(index).address().toString();
          ui->chat\_10->append( localAdapters.at(index).address().toString());
          qDebug() << " local adapater name " << localAdapters.at(index).name();
          ui->chat\_10->append( localAdapters.at(index).name());
      }
      
      Mircea NeacsuM Offline
      Mircea NeacsuM Offline
      Mircea Neacsu
      wrote on last edited by
      #2

      This should work:

      for(auto& adapter : localAdapters)
      {
      qDebug()<< " local adapter address" << adapter.address().toString();
      ui->chat_10->append( adapter.address().toString());
      qDebug() << " local adapater name " << adapter.name();
      ui->chat_10->append( adapter.name());
      }

      Mircea

      1 Reply Last reply
      0
      • L Lost User

        I like to learn how to use "foreach". I have posted similar request on another forum and got "RTFM" Could somebody REWRiTE the attached using "foreach" ? ..and a simple explanation of code used ( called "comments" ) would be nice. I need to start with "foreach" before goto "new style" of "for". Thanks

        localAdapters = QBluetoothLocalDevice::allDevices();
        count = localAdapters.count();
        ui->chat_10->clear();

        qDebug()<< " localAdapters  count " << QString::number(count);
        for(int index = 0; index < count; index++)
        {
            qDebug()<< " local adapter address" << localAdapters.at(index).address().toString();
            ui->chat\_10->append( localAdapters.at(index).address().toString());
            qDebug() << " local adapater name " << localAdapters.at(index).name();
            ui->chat\_10->append( localAdapters.at(index).name());
        }
        
        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        Try (not tested)

        auto localAdapters = QBluetoothLocalDevice::allDevices();
        ui->chat_10->clear();
        qDebug()<< " localAdapters count " << QString::number(localAdapters.count());
        for (const auto & device : localAdapters)
        {
        qDebug()<< " local adapter address" << device.address().toString();
        ui->chat_10->append( device.address().toString());
        qDebug() << " local adapater name " << device.name();
        ui->chat_10->append( device.name());
        }

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

        In testa che avete, signor di Ceprano?

        L 1 Reply Last reply
        0
        • L Lost User

          I like to learn how to use "foreach". I have posted similar request on another forum and got "RTFM" Could somebody REWRiTE the attached using "foreach" ? ..and a simple explanation of code used ( called "comments" ) would be nice. I need to start with "foreach" before goto "new style" of "for". Thanks

          localAdapters = QBluetoothLocalDevice::allDevices();
          count = localAdapters.count();
          ui->chat_10->clear();

          qDebug()<< " localAdapters  count " << QString::number(count);
          for(int index = 0; index < count; index++)
          {
              qDebug()<< " local adapter address" << localAdapters.at(index).address().toString();
              ui->chat\_10->append( localAdapters.at(index).address().toString());
              qDebug() << " local adapater name " << localAdapters.at(index).name();
              ui->chat\_10->append( localAdapters.at(index).name());
          }
          
          U Offline
          U Offline
          unitedsol zain
          wrote on last edited by
          #4

          Here is an example of how the code you provided can be rewritten using the foreach loop:

          Copy code

          localAdapters = QBluetoothLocalDevice::allDevices();
          count = localAdapters.count();
          ui->chat_10->clear();

          qDebug()<< " localAdapters count " << QString::number(count);

          // foreach loop to iterate through the local adapters
          foreach (QBluetoothLocalDevice localAdapter, localAdapters) {
          qDebug()<< " local adapter address" << localAdapter.address().toString();
          ui->chat_10->append( localAdapter.address().toString());
          qDebug() << " local adapater name " << localAdapter.name();
          ui->chat_10->append( localAdapter.name());
          }

          In this example, the foreach loop is used to iterate through each QBluetoothLocalDevice object in the localAdapters list. The variable localAdapter is declared as the loop variable and will take on the value of each object in the list as the loop iterates through it. The foreach loop is equivalent to the for loop. The code inside the loop is the same as the original code, it is just that the variable localAdapter is used instead of the variable localAdapters.at(index).

          In general, foreach is used to iterate over the items in a container, such as a list or an array. It is a simpler and more readable way to write a loop than a traditional for loop.

          1 Reply Last reply
          0
          • CPalliniC CPallini

            Try (not tested)

            auto localAdapters = QBluetoothLocalDevice::allDevices();
            ui->chat_10->clear();
            qDebug()<< " localAdapters count " << QString::number(localAdapters.count());
            for (const auto & device : localAdapters)
            {
            qDebug()<< " local adapter address" << device.address().toString();
            ui->chat_10->append( device.address().toString());
            qDebug() << " local adapater name " << device.name();
            ui->chat_10->append( device.name());
            }

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

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

            Somebody send me a private message to which I am unable to reply... I want to make sure he /she knows I got it and appreciate it. I did rewrite few lines of code using both "foreach" and "for" , works much gooder then the old "indexing" stuff... Thanks

            J 1 Reply Last reply
            0
            • L Lost User

              Somebody send me a private message to which I am unable to reply... I want to make sure he /she knows I got it and appreciate it. I did rewrite few lines of code using both "foreach" and "for" , works much gooder then the old "indexing" stuff... Thanks

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #6

              Better is subjective. For example if you modify the existing code to the following line then you should continue to use the existing solution

              qDebug()<< "index=" << i << " local adapter address" << localAdapters.at(index).address().toString();

              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