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. QVector of QPair<int, QString> does not sort the elements C++
QtWS25 Last Chance

QVector of QPair<int, QString> does not sort the elements C++

Scheduled Pinned Locked Moved Unsolved General and Desktop
qpairqvectorsortalgorithmsc++
2 Posts 2 Posters 533 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 last edited by
    #1

    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 bird

    After sort it should be:
    1 cat
    1 bird
    3 mouse
    5 dog
    6 rabbit

    But 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");
        }
    }
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

      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?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      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