QVector of QPair<int, QString> does not sort the elements C++
Unsolved
General and Desktop
-
Hello,
I have a Problem. I want to sort by first Element of QPair in a QVector.
Look at the code.Before sort:
5 dog
1 cat
3 mouse
6 rabbit
1 birdAfter sort it should be:
1 cat
1 bird
3 mouse
5 dog
6 rabbitBut instead nothing happend. I dont know why...
PS: safeIn() is a Function which safes values of the QVecors in a File.
QVector<QPair<int, QString>> zip(QVector<int> algorithm, QVector<QString> language) { QVector<QPair<int, QString>> zipped; for(int i = 0; i < language.size(); ++i) { zipped.push_back(qMakePair(algorithm[i], language[i])); } return zipped; } void unzip(QVector<QPair<int,QString>> zipped, QVector<int> algorithm, QVector<QString> language) { for(int i = 0; i < language.size(); i++) { algorithm[i] = zipped[i].first; language[i] = zipped[i].second; } } bool sortbyFirst(const QPair<int, QString> &a, const QPair<int, QString> &b) { return a.first < b.first; } void sort() { QVector<int> algorithm = {5, 1, 3, 6, 1}; QVector<QString> language = {dog, cat, mouse, rabbit, bird}; QVector<QPair<int, QString>> zipped = zip(algorithm, language); sort(zipped.begin(), zipped.end(), sortbyFirst); unzip(zipped, algorithm, language); for(int i = 0; i < language.size(); i++) { safeIn(QString::number(algorithm[i]), "C:/User/Desktop/algorithm.txt"); safeIn(language[i], "C:/User/Desktop/animals.txt"); } }
-
No sort problem but a basic c(++) problem - you pass the parameters to unzip() by value instead by reference so how should the those variables be updated outside?