38 lines
1 KiB
C++
38 lines
1 KiB
C++
#include <iostream>
|
|
#include <ranges>
|
|
#include <vector>
|
|
|
|
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) {
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
|
|
#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) {
|
|
if (sum + x < n) {
|
|
sum += x;
|
|
return true;
|
|
}
|
|
return false;
|
|
});
|
|
|
|
return std::vector<int>(result.begin(), result.end());
|
|
}
|
|
#endif
|