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