fork download
  1. #include <vector>
  2. #include <utility>
  3. #include <iostream>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. template< typename T >
  9. std::pair< T, T > swap_members( std::pair< T, T > pair )
  10. {
  11. using std::swap;
  12. swap( pair.first, pair.second );
  13. return pair;
  14. }
  15.  
  16. template< typename T, typename U >
  17. bool has_same_first( const std::pair< T, U >& lhs, const std::pair< T, U >& rhs)
  18. {
  19. return lhs.first == rhs.first;
  20. }
  21.  
  22. template< typename T, typename U >
  23. bool has_smaller_first( const std::pair< T, U >& lhs, const std::pair< T, U >& rhs)
  24. {
  25. return lhs.first < rhs.first;
  26. }
  27.  
  28. int main() {
  29. vector<pair<int, int>> v1 = { {10, 2}, {10, 1}, {3, 7} };
  30. vector<pair<int, int>> v2 = { {1, 3}, {9, 10} };
  31.  
  32. std::transform( std::begin( v2 ), std::end( v2 ), std::begin( v2 ), swap_members< int > );
  33.  
  34. // Insere as coordenadas do novo vetor no original
  35. // e ordena de tal modo que todos os elementos com a primeiro int igual estão juntos
  36. // e o primeiro destes elementos é quem tem o maior segundo inteiro.
  37. v2.insert( std::end( v2 ), std::begin( v1 ), std::end( v1 ) );
  38. std::sort( std::begin( v2 ), std::end( v2 ), std::greater< std::pair< int, int > >{} );
  39.  
  40. // Garante que apenas a primeira ocorrecia de cada valor do primeiro inteiro será mantida.
  41. auto new_end = std::unique( std::begin( v2 ), std::end( v2 ), has_same_first< int, int > );
  42. v2.erase( new_end, std::end( v2 ) );
  43.  
  44. for ( auto&& elem : v2 )
  45. {
  46. std::cout << "( " << elem.first << ", " << elem.second << " )";
  47. }
  48. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
( 10, 9 )( 3, 7 )