cpp_prac/junk/4.cc

39 lines
1 KiB
C++
Raw Normal View History

2025-01-10 08:15:31 +01:00
#include <iostream>
#include <ranges>
2025-01-11 17:54:29 +01:00
#include <vector>
2025-01-10 08:15:31 +01:00
template <typename InputIt, typename OutputIt, typename Pred>
std::pair<InputIt, OutputIt> copy_while(InputIt first, InputIt last,
OutputIt out, Pred p);
std::vector<int> take_while_sum_less_than(std::vector<int> &v, int n) {
2025-01-11 17:54:29 +01:00
std::vector<int> out;
int acc = 0;
while (true) {
if (v.back() + acc > n) {
int next = v.back();
out.push_back(n);
v.pop_back();
}
2025-01-10 08:15:31 +01:00
}
}
#if __cplusplus > 202002L
// #include <ranges>
// #include <numeric>
std::vector<int> take_while_sum_less_than(const std::vector<int> &v, int n) {
int sum = 0;
auto result = v | std::ranges::views::take_while([&sum, n](int x) {
2025-01-11 17:54:29 +01:00
if (sum + x < n) {
sum += x;
return true;
}
return false;
});
2025-01-10 08:15:31 +01:00
return std::vector<int>(result.begin(), result.end());
}
#endif