Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. void imprimir(const vector <int> &v){
  8.     for(int i = 0; i < int(v.size()); i++)
  9.         cout << v[i] << " ";
  10.     cout << endl;
  11. }
  12.  
  13. bool comparar(const int &a, const int &b){ ///a y b son dos elementos del vector que estan siendo comparados, como el vector es de int recibimos a y b como enteros
  14.     if(a > b)
  15.         return true;
  16.  
  17.     return false;
  18. }
  19.  
  20. int main()
  21. {
  22.     vector <int> v = {10, 1, 32, 99, 11, 4};
  23.  
  24.     sort(v.begin(), v.end()); ///Sort normal ordena de menor a mayor
  25.  
  26.     imprimir(v);
  27.  
  28.     sort(v.begin(), v.end(), comparar); ///Si utilizo el comparador puedo decirle que ordene como yo quiero
  29.  
  30.     imprimir(v);
  31.  
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement