Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. // Example program
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. #include <algorithm>
  6.  
  7. int main()
  8. {
  9.   std::vector<int> v = {7, 4, 9,2, 1, 5, 6, 3, 8};
  10.  
  11.   std::vector<double> values = {4, 5, 3};
  12.     auto maxIt = std::max_element(values.begin(), values.end(), [](const auto& a, const auto& b) { return std::max(-a, a) < std::max(-b, b); });
  13.     auto maxIndex = std::distance(values.begin(), maxIt); // index of X=0, Y=1 or Z=2 coordiante
  14.     bool asc = values[maxIndex] >= 0; // sort ascending if the delta value is positive, else descending
  15.    
  16.     std::cout << maxIndex << std::endl;
  17.     std::cout << asc << std::endl;
  18.    
  19.   auto sortFunc = [](const auto left, const auto right, bool asc) { return asc ? left < right : left > right; };
  20.  
  21.   std::sort(v.begin() + 1, v.end() - 1, [&sortFunc, &asc](const auto&a, const auto&b){ return sortFunc(a, b, asc); });
  22.   for(const auto& i : v)
  23.   {
  24.      std::cout << i << std::endl;
  25.   }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement