Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9122855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:12:04+00:00 2026-06-17T06:12:04+00:00

I have a QList<MyData> , where MyData have 2 member, int id (unique) and

  • 0

I have a QList<MyData>, where MyData have 2 member, int id (unique) and QString name. I want to remove all duplicate entries based on name, and that entry must the highest id between other object that having the same name. Any suggestion of how to doing it in the fastest way? Performance is a very important factor here.

Some of my idea after Google-ed for the whole day:

  • qStableSort() it based on id (descending), then loop through the QList, then for each entry, copy the entry to another new QList when the name is not exist on the new QList
  • use QList::toSet (which remove all duplicate entry), and provide operator==() and a qHash() implementation based on name, but the unique entry may not have the highest id
  • use std::list::unique, but I’m not sure how it works.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:12:05+00:00Added an answer on June 17, 2026 at 6:12 am

    std::list::unique can take as argument a function with the following properties:

    Binary predicate that, taking two values of the same type than those
    contained in the list, returns true to remove the element passed as
    first argument from the container, and false otherwise.
    This shall be a function pointer or a function object.

    So in your case you could use the folllowing function:

    bool shouldRemove(MyData first, MyData second)
    {
        // remove only if they have the same name and the first id
        // is smaller than the second one 
        return ( first.name == second.name && 
                 first.id <= second.id ); 
    }
    

    To call it simply do,

    std::list<MyData> myList = qlist.toStdList();
    myList.unique(shouldRemove)
    

    Notice that you need to sort first the std::list

    Edit

    It seems that you can use std::unique with Qt containers (if Qt is built with STL support). So in this case you could do the following:

    // lessThan is a function that sorts first by name and then by id
    qSort(qList.begin(), qList.end(), lessThan );
    QList<MyData>::iterator it = std::unique (qList.begin(), qList.end(), shouldRemove);
    qList.erase(it, qList.end());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class, Car, that inherits a QList< int >. I am wondering
I have a collection of QList<int> 's that I need to calculate the union
I have a QList list. I want to insert it on the database. I
Pretty new to threading and I have this QList that the threads share between
If have a class, Book, that just inherited QList< char > , I am
I have a QList variable. How can I erase all the elements starting from
I have a method to convert dec to bin QList<bool> widgetInput::decToBin(int number) { int
I don't have much experience with QT and this problem came out today. QList<int>
I have a levelObjects object that's a QList of QVariants of QMaps: QList< QVariant
I have q method that shown at below: void MainWindow::slotResults( const QList<QSqlRecord>& records )

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.