fork download
  1. #include <iterator>
  2. #include <vector>
  3. #include <iostream>
  4.  
  5. template<typename InputIt, typename ForwardIt, typename UnaryPred>
  6. void copy_until(InputIt start, InputIt end, ForwardIt d_first, UnaryPred p) {
  7. while(start != end && p(*start))
  8. *d_first++ = *start++;
  9. }
  10.  
  11. int main() {
  12. std::vector<int> arr;
  13. int count = 0;
  14. copy_until(std::istream_iterator<int>(std::cin),
  15. std::istream_iterator<int>(),
  16. back_inserter(arr), [&](int) {
  17. return ++count < 3;
  18. });
  19.  
  20. for(int i : arr)
  21. std::cout << i << ' ';
  22. }
Success #stdin #stdout 0s 15240KB
stdin
1 2 3 4 5
stdout
1 2