Advertisement
Caminhoneiro

Sorting (STL)

Jul 4th, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. // Sorting.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <vector>
  6. #include <algorithm>
  7. #include <cmath> // for abs
  8. #include "Employee.h"
  9. #include <random>
  10.  
  11. using namespace std;
  12.  
  13. int main()
  14. {
  15.     vector<int> v{ 4,1,0,1,-2,3,7,-6,2,0,0,-9,9 };
  16.     auto v2 = v;
  17.     sort(begin(v2), end(v2));
  18.     sort(begin(v2), end(v2), [](int elem1, int elem2) {return elem1 > elem2; }); //Less to major
  19.     sort(begin(v2), end(v2), [](int elem1, int elem2) {return abs(elem1) > abs(elem2); }); //Greater to minor
  20.  
  21.     std::vector<Employee> staff{
  22.         { "Kate", "Gregory", 1000 },
  23.         { "Obvious", "Artificial", 2000 },
  24.         { "Fake", "Name", 1000 },
  25.         { "Alan", "Turing", 2000 },
  26.         { "Grace", "Hopper", 2000 },
  27.         { "Anita", "Borg", 2000 }
  28.     };
  29.  
  30.     //std::sort(begin(staff), end(staff)); //- only works if operator< defined for Employee
  31.  
  32.     std::sort(begin(staff), end(staff),
  33.         [](Employee e1, Employee e2) {return e1.getSalary() < e2.getSalary(); });
  34.     std::sort(begin(staff), end(staff),
  35.         [](Employee e1, Employee e2) {return e1.getSortingName() < e2.getSortingName(); });
  36.  
  37.     std::sort(begin(staff), end(staff),
  38.         [](Employee e1, Employee e2) {return e1.getSortingName() < e2.getSortingName(); });
  39.     std::stable_sort(begin(staff), end(staff),
  40.         [](Employee e1, Employee e2) {return e1.getSalary() < e2.getSalary(); });
  41.  
  42.     auto sorted = is_sorted(begin(v2), end(v2));
  43.     sorted = is_sorted(begin(v2), end(v2),[](int elem1, int elem2) {return abs(elem1) > abs(elem2); });
  44.  
  45.     int high = *(max_element(begin(v), end(v)));
  46.     int low = *(min_element(begin(v), end(v)));
  47.     sort(begin(v2), end(v2));
  48.     low = *begin(v2);
  49.     high = *(end(v2)-1);
  50.     int positive = *upper_bound(begin(v2), end(v2), 0);
  51.  
  52.     std::sort(begin(staff), end(staff),
  53.         [](Employee e1, Employee e2) {return e1.getSortingName() < e2.getSortingName(); });
  54.  
  55.     auto p = std::lower_bound(begin(staff), end(staff), "Gregory, Kate",
  56.             [](Employee e1, std::string n) {return e1.getSortingName() < n; });
  57.     int sal = p->getSalary();
  58.  
  59.     random_device randomdevice;
  60.     mt19937 generator(randomdevice());
  61.    
  62.     shuffle(begin(v2), end(v2), generator);
  63.  
  64.     partial_sort(begin(v2), find(begin(v2), end(v2), 4), end(v2));
  65.  
  66.     int breakpoint = *is_sorted_until(begin(v2), end(v2));
  67.  
  68.     vector<int> v3(3);
  69.     partial_sort_copy(begin(v), end(v), begin(v3), end(v3));
  70.  
  71.     v2 = {1,5,4,2,9,7,3,8,2};
  72.     int i = *(begin(v2) + 4);
  73.     nth_element(begin(v2), begin(v2)+4, end(v2));
  74.     i = *(begin(v2) + 4);
  75.  
  76.  
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement