Created
August 31, 2015 06:20
-
-
Save usagi/faff1fb2346827c9d437 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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