fork download
  1. #include <utility>
  2. #include <vector>
  3. #include <map>
  4. #include <iostream>
  5.  
  6. using coords = std::pair<int,int> ;
  7. using line = std::pair<coords,coords> ; // start-end coordinates
  8.  
  9. std::vector<line> remove_pairs_with_the_same_destination(
  10. const std::vector<line>& srce_dest_pairs )
  11. {
  12. static const auto compare_dest = [] ( const line& a, const line& b )
  13. { return a.second < b.second ; } ;
  14. std::map< line, int, decltype(compare_dest) > counts(compare_dest) ;
  15. for( const line& p : srce_dest_pairs ) ++counts[p] ;
  16.  
  17. std::vector<line> result ;
  18. for( const auto& p : counts ) if( p.second == 1 ) result.push_back(p.first) ;
  19.  
  20. return result ;
  21. }
  22.  
  23. int main()
  24. {
  25. std::vector<line> seq =
  26. { { {0,7}, {0,2} }, { {1,2}, {3,4} }, { {5,6}, {0,2} }, { {7,2}, {7,8} }, { {8,6}, {0,2} } } ;
  27.  
  28. static const auto print = [] ( const std::vector<line>& sequence )
  29. {
  30. for( const auto& line : sequence )
  31. std::cout << line.first.first << ',' << line.first.second << " => "
  32. << line.second.first << ',' << line.second.second << '\n' ;
  33. };
  34.  
  35. print(seq) ;
  36. std::cout << "----------------\n" ;
  37. print( remove_pairs_with_the_same_destination(seq) ) ;
  38. }
  39.  
Success #stdin #stdout 0s 3032KB
stdin
Standard input is empty
stdout
0,7 => 0,2
1,2 => 3,4
5,6 => 0,2
7,2 => 7,8
8,6 => 0,2
----------------
1,2 => 3,4
7,2 => 7,8