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. sort a vector and shuffle all similar values
Forum Update on Monday, May 27th 2025

sort a vector and shuffle all similar values

Scheduled Pinned Locked Moved Unsolved General and Desktop
shufflevectorqvectorc++sort
4 Posts 2 Posters 714 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.
  • M Offline
    M Offline
    MEsc
    wrote on 28 Nov 2022, 14:00 last edited by MEsc
    #1

    Hello, I have a Question. I am programming an algorithm for a vocabel trainer.
    Example:

    QVector<QString> english = {mouse, cat, dog, cheese, house};
    QVector<int> value = {1, 0, 0, 3, 0};
    

    english is a vector where all english vocabels are stored (you can add vocabels)

    value is a vector where your right answers are stored.

    Here:
    mouse = 1
    cat = 0
    dog = 0
    cheese = 3
    house = 0

    If you type in a wrong vocabel. Value of it goes to 1.
    If you add a new vocabel and you never type in this vocabel. Value is 0.
    If you are right it add 1 to the existing value.

    The easy part is to sort this data.
    Here it would be:

    QVector<QString> english = {cat, dog, house, mouse, cheese};
    QVector<int> value = {0, 0, 0, 1, 3};
    

    But I want to sort all similar values random. That means.

    Every vocabel here with value 0 are sorted to the begin of the vector but random.
    Here the first three values in the vector can change position, but only up to value[2].

    Example:
    cat could also be at english[1] or [2]
    dog could also be at english[0] or [2]
    house could also be at english[0] or [1]

    Is this possible?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 28 Nov 2022, 17:06 last edited by
      #2

      You can find the range of elements in a sorted vector that have the same value using std::equal_range.
      You can then shuffle them using std::random_shuffle.

      M 1 Reply Last reply 28 Nov 2022, 20:19
      2
      • C Chris Kawa
        28 Nov 2022, 17:06

        You can find the range of elements in a sorted vector that have the same value using std::equal_range.
        You can then shuffle them using std::random_shuffle.

        M Offline
        M Offline
        MEsc
        wrote on 28 Nov 2022, 20:19 last edited by
        #3

        @Chris-Kawa can you give an example please?

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on 28 Nov 2022, 20:23 last edited by Chris Kawa
          #4

          There are examples and detailed descriptions on both pages I linked.

          1 Reply Last reply
          1

          1/4

          28 Nov 2022, 14:00

          • Login

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