fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <tuple>
  4. #include <cstdint>
  5.  
  6. template<class T> using Pair= std::tuple<T, T>;
  7. template<class T> using PType = std::vector<Pair<T>>;
  8. template<class T> using DType = std::vector<T>;
  9.  
  10. template<class T,class F>
  11. DType<T> overlappingPairsCollect(const DType<T>& D,const F& Fun) {
  12. PType<T> P;
  13.  
  14. T Head = D.front();
  15. for (std::size_t i = 1; i < D.size(); i++) {
  16. const T& Next = D[i];
  17. P.push_back({ Head,Next });
  18. Head = Next;
  19. }
  20. DType<T> R;
  21. for (auto& o : P) {
  22. R.push_back( Fun(std::get<0>(o), std::get<1>(o)));
  23. }
  24.  
  25. return R;
  26. }
  27.  
  28. template<class T,class F>
  29. DType<T> MakeHoge(const DType<T>& D,const F& Fun) {
  30. DType<T> R;
  31. for (std::size_t i = 0; i < D.size()-1; i++) {
  32. R.push_back(Fun(D[i], D[i + 1]));
  33. }
  34. return R;
  35. }
  36. int main() {
  37.  
  38. DType<std::int64_t> D{ 3,1,4,1,5,9,2,6,5 };
  39.  
  40. //auto R = overlappingPairsCollect(D, [](auto& A, auto& B) { return B - A; });
  41. auto R = MakeHoge(D, [](auto& A, auto& B) { return B - A; });
  42. for (auto& o : R) {
  43. std::cout << o << ',';
  44. }
  45. std::cout << std::endl;
  46.  
  47. return 0;
  48. }
Success #stdin #stdout 0s 4560KB
stdin
Standard input is empty
stdout
-2,3,-3,4,4,-7,4,-1,