#include <algorithm>
#include <iterator>
#include <vector>
#include <iostream>
template <class InputIt, class OutputIt>
OutputIt copy_n_max(InputIt begin, InputIt end, OutputIt last, size_t count)
{
return std::copy_if(begin,
end,
last,
[&count](typename std::iterator_traits<InputIt>::reference) -> bool
{
if (count > 0)
{
--count;
return true;
}
return false;
});
}
int main(int, char const *[])
{
{
std::vector<long> input{ 1, 2, 3, 4, 5, 6};
std::vector<long> output_short;
std::vector<long> output_long;
copy_n_max(input.begin(), input.end(), std::back_inserter(output_short), 3);
copy_n_max(input.begin(), input.end(), std::back_inserter(output_long), input.size());
std::cout << output_short.size() << std::endl;
std::cout << output_long.size() << std::endl;
}
{
std::vector<bool> input{ true, true, true, true, false };
std::vector<bool> output_short;
std::vector<bool> output_long;
copy_n_max(input.begin(), input.end(), std::back_inserter(output_short), 3);
copy_n_max(input.begin(), input.end(), std::back_inserter(output_long), input.size());
std::cout << output_short.size() << std::endl;
std::cout << output_long.size() << std::endl;
}
return 0;
}
I2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGl0ZXJhdG9yPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8aW9zdHJlYW0+CgoKdGVtcGxhdGUgPGNsYXNzIElucHV0SXQsIGNsYXNzIE91dHB1dEl0PgpPdXRwdXRJdCBjb3B5X25fbWF4KElucHV0SXQgYmVnaW4sIElucHV0SXQgZW5kLCBPdXRwdXRJdCBsYXN0LCBzaXplX3QgY291bnQpCnsKCXJldHVybiBzdGQ6OmNvcHlfaWYoYmVnaW4sIAoJICAgICAgICAgICAgICAgICAgICBlbmQsIAoJICAgICAgICAgICAgICAgICAgICBsYXN0LCAKCSAgICAgICAgICAgICAgICAgICAgWyZjb3VudF0odHlwZW5hbWUgc3RkOjppdGVyYXRvcl90cmFpdHM8SW5wdXRJdD46OnJlZmVyZW5jZSkgLT4gYm9vbCAKCSAgICAgICAgICAgICAgICAgICAgewoJICAgICAgICAgICAgICAgICAgICAJaWYgKGNvdW50ID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