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 8650049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:43:11+00:00 2026-06-12T13:43:11+00:00

I was trying to sort a vector of objects by values stored in a

  • 0

I was trying to sort a vector of objects by values stored in a map, using the STL sort() function. To my great surprise, my algorithm was running in quadratic time. I simplified it as much as I could, trying to pinpoint an apparent bug, but to no avail. Here is the simplified version:

#include <map>
#include <vector>
#include <algorithm>

using namespace std;

struct a{
  map<a*,float> vals;
  bool operator()(a* a1, a* a2){return (vals[a1]>vals[a2]);}
  void asort();
};

void a::asort(){
  vector<a*> v;
  map<a*,float>::iterator it = vals.begin();
  for(;it!=vals.end();it++){v.push_back((*it).first);}
  sort(v.begin(),v.end(),*this);
}

int main(){
  a a0;
  int imax=8000;
  for(int i=0;i<imax;i++){a0.vals[new a]=rand();}
  a0.asort();
}

When I run it for imax=2000, 4000, 8000, it takes about 1s, 4s, 18s respectively. How is this possible? Why don’t I get the expected imax*log(imax) dependence? I have limited experience with C++, please help! Thanks!

Update: thank you Xeo, Rick and everyone who responded. As Xeo and Rick explained, the problem is that the comparator (in my case struct a with the map containing values) gets copied on each comparison, hence the computational complexity O(imax^2 log(imax)). One way around this that I can see (so that changes to my code are minimal) is to use a pointer to the map, namely map<a*,float>* vals, instead of map<a*,float> vals. Then the map copying is avoided, and the complexity is back to O(imax log(imax)). Thanks a lot!

  • 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-12T13:43:13+00:00Added an answer on June 12, 2026 at 1:43 pm

    std::sort takes its predicate by value, meaning

    sort(v.begin(),v.end(),*this);
    //                     ^^^^^
    

    will copy the contained map.

    Next, you’re doing a map lookup twice during the comparision, which is O(log N), while it’s expected that pred(a,b) is a constant operation.

    You can fix this by defining a seperate comparator for std::sort and using std::unordered_map (C++11).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to sort a vector of objects using a predicate function and I'm
I am trying to sort a Vector list using the Java Collections frameworks. This
I am trying to sort a vector below using boost::phoenix library. The class Foo
I have a vector of Student objects which I want to sort using #include
I'm trying to sort a Vector in java but my Vector is not a
Trying to sort this array of objects according to (1) depth and (2) weight,
I am trying to sort pixel values of an image (example 80x20) from lowest
I ran across an issue whenever I was trying to sort a vector of
I'm new to pl/sql ! I'm trying to sort a table of records, using
I am attempting to implement a function that sorts a randomly generated vector using

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.