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

  • Home
  • SEARCH
  • 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 8536871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:46:00+00:00 2026-06-11T10:46:00+00:00

I have std::set<std::pair<float,float>> which represents points on map ( 2d , x and y

  • 0

I have std::set<std::pair<float,float>> which represents points on map ( 2d , x and y value) and I have one point with values x1 and y1. How to sort set in ascending order by distance from point ( x1,y1) ?

  • 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-11T10:46:01+00:00Added an answer on June 11, 2026 at 10:46 am

    std::set is an ordered container, and ordering happens upon insertion, depending on a sorting criteria which can be specified with a second template argument. So use a set with a predicate which returns true or false based on the distance to the reference point.

    struct DistanceCompare
    {
      DistanceCompare(const std::pair<float,float>& point) : point_(point) {} 
      bool operator()(const std::pair<float,float>& lhs, 
                      const std::pair<float,float>& rhs) const
      {
        return distance2(lhs) < distance2(rhs);
      };
    
     private:
      float distance2(const std::pair<float,float>& point) const
      {
        // calculate distance squared between point and point_
        const float x = point.first  - point_.first;
        const float y = point.second - point_.second;
        return x*x + y*y;
    
      }
      std::pair<float, float> point_;
    };
    
    ....
    std::pair<float,float> refPoint = ....;
    DistanceCompare comp(refPoint);
    std::set<std::pair<float, float>, DistanceCompare> pointSet(comp);
    

    It is enough to compare the distance squared, thus avoiding calls to std::sqrt.

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

Sidebar

Related Questions

I have a class which has one private member: std::set<unsigned long> Sset; And I
I have an std::map<std::string, float> so I can do quick lookups for float values
I have a std::set which is of type point struct point { int x;
Possible Duplicate: remove_if equivalent for std::map I have a set of strings: set <wstring>
Imagine you have an std::list with a set of values in it. For demonstration's
I have a class named Particle which has a std::set as a member. The
I have some C++ methods that have std::set<std::string> as argument or return value. I
I have an std::set with the Compare class which requires additional parameter to compare
Suppose I have a set of values, stored in a std::set: {1, 2, 6,
I have a std::set<Foo> , and I'd like to update some value of an

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.