From 99847d2bf132854fffa019bab19818768102ccad Mon Sep 17 00:00:00 2001 From: Howard Hinnant Date: Sun, 27 Feb 2011 20:55:39 +0000 Subject: [PATCH] Fix copy_n to increment only n-1 times for an input iterator. This works much better with std::istream_iterator(std::cin). Credit: Matan Nassau. llvm-svn: 126581 --- libcxx/include/algorithm | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm index d91c57c11098..4909221bb09f 100644 --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -1559,8 +1559,17 @@ typename enable_if >::type copy_n(_InputIterator __first, _Size __n, _OutputIterator __result) { - for (; __n > 0; --__n, ++__first, ++__result) + if (__n > 0) + { *__result = *__first; + ++__result; + for (--__n; __n > 0; --__n) + { + ++__first; + *__result = *__first; + ++__result; + } + } return __result; }