c++11 - sorting elements in a vector of curves -


i have class point represents x , y coordinate , class curve have 2 point, start point , end point.

class point { public:     double x{0.0}, y{0.0};     //......... }  class curve { public:     point start, end;     //......... } 

i have vector of curves, needs sorted. start point of 1 curve equals end point of other. output curve (keeping 1 curve after other) can open curve or close curve(always continuous curve).

current logic lot of loops , 2/3 vectors.. there way implement same using standard algorithms(c++11).

assuming first element of vector starting point of path , there 1 solution, following lines job:

bool operator!=(point& a,point& b) {     return !(a.x == b.x && b.y == a.y); }  bool operator==(point& a, point& b) {     return (a.x == b.x && b.y == a.y); }  void order(std::vector<curve>& vin) {     auto = vin.begin();     auto end = vin.end();     while (it+1 != end) {         if (it->end != (it + 1)->start) {             std::swap(*(it + 1), *std::find_if(it + 2, end, [it](curve& c){ return c.start == it->end ;  }));         }         ++it ;     } } 

if need find first element, define predicate is_the_beginning , similar call swap before loop:

bool is_the_beginning(curve& c) {     if ( ... )  return true;     else return false ; } std::swap(*it, *std::find_if(it+1, end, is_the_beginning ) ) ; 

maybe need take account precision of double operator == , !=. can replace them functions


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -