sort a vector and shuffle all similar values
-
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 = 0If 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?
-
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. -
@Chris-Kawa can you give an example please?
-
There are examples and detailed descriptions on both pages I linked.