Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Does deleting something from QStringList in middle of for loop will have affect some thing.
Forum Updated to NodeBB v4.3 + New Features

Does deleting something from QStringList in middle of for loop will have affect some thing.

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 388 Views
  • 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.
  • A Offline
    A Offline
    Ayush Gupta
    wrote on last edited by
    #1

    I have below scenario

    one QStringList test;

    now below code

    for(const QString &str : test)
    {
    if( str .contains("a))
    {
    test.remove(str);
    }
    }

    My concern i am deleting something from list test in middle of for loop of iterating test list
    Will it will invalidate iterators something like this?

    jsulmJ 1 Reply Last reply
    0
    • A Ayush Gupta

      But it seems iterators will be invalidated.How can I handle that

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #4

      @Ayush-Gupta Please read the link I posted:
      "Removes the item associated with the iterator pos from the list, and returns an iterator to the next item in the list (which may be end()).". So, nothing is invalidated.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      7
      • A Ayush Gupta

        I have below scenario

        one QStringList test;

        now below code

        for(const QString &str : test)
        {
        if( str .contains("a))
        {
        test.remove(str);
        }
        }

        My concern i am deleting something from list test in middle of for loop of iterating test list
        Will it will invalidate iterators something like this?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @Ayush-Gupta You should use iterators in such a case for your loop and https://doc.qt.io/qt-5/qlist.html#erase to remove elements inside the loop.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        5
        • A Offline
          A Offline
          Ayush Gupta
          wrote on last edited by
          #3

          But it seems iterators will be invalidated.How can I handle that

          jsulmJ 1 Reply Last reply
          0
          • A Ayush Gupta

            But it seems iterators will be invalidated.How can I handle that

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by jsulm
            #4

            @Ayush-Gupta Please read the link I posted:
            "Removes the item associated with the iterator pos from the list, and returns an iterator to the next item in the list (which may be end()).". So, nothing is invalidated.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            7

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved