Skip to content

Instantly share code, notes, and snippets.

@usagi
Created August 31, 2015 06:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save usagi/faff1fb2346827c9d437 to your computer and use it in GitHub Desktop.
Save usagi/faff1fb2346827c9d437 to your computer and use it in GitHub Desktop.
#include <queue>
#include <vector>
#include <cmath>
#include <iostream>
struct p{ double x,y; char data; };
auto main() -> int
{
std::vector<p> ps
{ { 0, 0, 'a' }
, { 0, 1, 'b' }
, { 1, 1, 'c' }
, { -0.5, -0.5, 'd' }
, { 0, 0.75, 'e' }
};
const p n{ 0.5, 0.5, 'N' };
const auto comparer = [n](const p* pa, const p* pb )
{
const auto distance = [n](const p* px)
{
const auto dx = px->x - n.x;
const auto dy = px->y - n.y;
return std::sqrt( dx * dx + dy * dy );
};
return distance(pa) > distance(pb);
};
std::priority_queue<const p*, std::vector<const p*>, decltype(comparer)> pp(comparer);
for ( const auto& p : ps )
{
pp.emplace( &p );
std::cout << p.data;
}
std::cout << "\n";
while( not pp.empty() )
{
const auto rp = pp.top();
std::cout << rp->data;
pp.pop();
}
std::cout << "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment