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. converting list of strings representing numbers to set
Qt 6.11 is out! See what's new in the release blog

converting list of strings representing numbers to set

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 613 Views 3 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 user4592357
    #1

    i have a list of numbers, i need to join the numbers into a string by _ character.
    e.g.

    QStringList lstStrings{"1", "2", "3"};
    std::cout << QStringList(lstStrings.toSet().toList()).join('_').toStdString();
    

    this prints 3_1_2. why does this happen?
    i expect 1_2_3 to be printed.

    JonBJ 1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #2

      Please take some time to read the documentation:

      QSet is unordered, so an iterator's sequence cannot be assumed to be predictable. If ordering by key is required, use a QMap.

      You can try

      lstStrings.removeDuplicates();
      lstStrings.sort();
      

      But still, this sort will only sort them as strings, not numbers.

      1 Reply Last reply
      2
      • U user4592357

        i have a list of numbers, i need to join the numbers into a string by _ character.
        e.g.

        QStringList lstStrings{"1", "2", "3"};
        std::cout << QStringList(lstStrings.toSet().toList()).join('_').toStdString();
        

        this prints 3_1_2. why does this happen?
        i expect 1_2_3 to be printed.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @user4592357 said in converting list of strings representing numbers to set:

        this prints 3_1_2. why does this happen?

        Because you went toSet().

        i expect 1_2_3 to be printed.

        All you needed for this is QStringList::join(). There is no sorting here.

        std::cout << lstStrings.join("_");
        
        U 1 Reply Last reply
        1
        • JonBJ JonB

          @user4592357 said in converting list of strings representing numbers to set:

          this prints 3_1_2. why does this happen?

          Because you went toSet().

          i expect 1_2_3 to be printed.

          All you needed for this is QStringList::join(). There is no sorting here.

          std::cout << lstStrings.join("_");
          
          U Offline
          U Offline
          user4592357
          wrote on last edited by
          #4

          @JonB
          the reason i used toSet() is that the items in my string list may be reordered, and i want to always get the same sequence from same items.
          i guess not sorting is fine as long as i get the same result for same input.

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

            Hi,

            You can use QCollator with the numerical mode activated for your numeric text sorting.

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

            1 Reply Last reply
            3

            • Login

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