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. removing elements from QStringList
Qt 6.11 is out! See what's new in the release blog

removing elements from QStringList

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 5.1k Views 2 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.
  • U Offline
    U Offline
    user4592357
    wrote on last edited by
    #1

    i have a similar code:

    QStringList lst;
    // fill lst with values
    
    std::vector<bool> valid(lst.size(), true);
    // set values to false if necessary
    
    // now, remove invalid elements
    int i = 0;
    for (auto val : lst)
    {
    	if (!valid[i])
    		lst.removeOne(val);
    	++i;
    }
    

    this is what i have now but i really don't like having the loop and doing this. what else can i do? i don't care if it's a Qt solution or STL algorithm or whatever.

    1 Reply Last reply
    0
    • Alvaro DenisA Offline
      Alvaro DenisA Offline
      Alvaro Denis
      wrote on last edited by Alvaro Denis
      #2

      Try this:

      int main(int argc, char *argv[]) {
        QStringList lst{"dfdfdfdf", "89", "d89fd8f&*", "dfdf", "ds"};
        lst.erase(std::remove_if(lst.begin(), lst.end(),
                       [](const QString &s) { return s.size() > 5; }), lst.end());
        qDebug() << lst;
      }
      

      Use your "valid" logic into the lambda.

      1 Reply Last reply
      5
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        To add to @Alvaro-Denis, you should make s a const reference to avoid needless copies.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        U 1 Reply Last reply
        3
        • SGaistS SGaist

          Hi,

          To add to @Alvaro-Denis, you should make s a const reference to avoid needless copies.

          U Offline
          U Offline
          user4592357
          wrote on last edited by
          #4

          @SGaist
          i know. i always do that. thanks

          1 Reply Last reply
          0
          • Alvaro DenisA Offline
            Alvaro DenisA Offline
            Alvaro Denis
            wrote on last edited by Alvaro Denis
            #5

            @user4592357 I was improve the code in my original response based on the @SGaist suggestion, but in general be careful with the "i always", specifically on lambda, consider this

            U 1 Reply Last reply
            0
            • Alvaro DenisA Alvaro Denis

              @user4592357 I was improve the code in my original response based on the @SGaist suggestion, but in general be careful with the "i always", specifically on lambda, consider this

              U Offline
              U Offline
              user4592357
              wrote on last edited by
              #6

              @Alvaro-Denis
              which part should have i be careful about? it says nothing about parameters, only captures.

              1 Reply Last reply
              0

              • Login

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