[Modern C++] range-based for loop

    Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

    • [Modern C++] range-based for loop

      Welcome to a simple tutorial to move on with modern c++

      Iterating for vectors looks like:

      C-Quellcode

      1. for (std::vector<GossipMenuItem>::iterator iter = Menu.begin(); iter != Menu.end(); ++iter)
      2. {
      3. Packet << iter->Id;
      4. Packet << iter->Icon;
      5. Packet << iter->Extra;
      6. Packet << uint32(iter->m_gBoxMoney); // money required to open menu, 2.0.3
      7. Packet << iter->Text;
      8. Packet << iter->m_gBoxMessage; // accept text (related to money) pop up box, 2.0.3
      9. }


      In modern c++ you can use the ranged-based for loop equivalent:

      C-Quellcode

      1. for (auto iter : Menu)
      2. {
      3. Packet << iter.Id;
      4. Packet << iter.Icon;
      5. Packet << iter.Extra;
      6. Packet << uint32(iter.m_gBoxMoney); // money required to open menu, 2.0.3
      7. Packet << iter.Text;
      8. Packet << iter.m_gBoxMessage; // accept text (related to money) pop up box, 2.0.3
      9. }