From fe51eb60a922726463e4fdad091f1682d544ad03 Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Sun, 25 Aug 2013 16:29:01 +0000 Subject: [PATCH] Rewrote iota_n to use pre-increment instead of post - now the same as iota. Added a test for 0 as well. [SVN r85465] --- include/boost/algorithm/cxx11/iota.hpp | 4 ++-- test/iota_test1.cpp | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/include/boost/algorithm/cxx11/iota.hpp b/include/boost/algorithm/cxx11/iota.hpp index b4f0daf..eb32390 100644 --- a/include/boost/algorithm/cxx11/iota.hpp +++ b/include/boost/algorithm/cxx11/iota.hpp @@ -63,8 +63,8 @@ void iota ( Range &r, T value ) template OutputIterator iota_n ( OutputIterator out, T value, std::size_t n ) { - while ( n-- > 0 ) - *out++ = value++; + for ( ; n > 0; --n, ++value ) + *out++ = value; return out; } diff --git a/test/iota_test1.cpp b/test/iota_test1.cpp index 080d3d7..747691f 100644 --- a/test/iota_test1.cpp +++ b/test/iota_test1.cpp @@ -42,11 +42,11 @@ void test_ints () { std::vector v; std::list l; - v.clear (); v.reserve ( 10 ); + v.clear (); v.resize ( 10 ); boost::algorithm::iota ( v.begin (), v.end (), 23 ); BOOST_CHECK ( test_iota_results ( v.begin (), v.end (), 23 )); - v.clear (); v.reserve ( 19 ); + v.clear (); v.resize ( 19 ); boost::algorithm::iota ( v, 18 ); BOOST_CHECK ( test_iota_results ( v, 18 )); @@ -54,6 +54,10 @@ void test_ints () { boost::algorithm::iota_n ( std::back_inserter(v), 99, 20 ); BOOST_CHECK ( test_iota_results ( v, 99 )); + v.clear (); + boost::algorithm::iota_n ( std::back_inserter(v), 99, 0 ); + BOOST_CHECK ( v.size() == 0 ); + /* l.clear (); l.reserve ( 5 ); boost::algorithm::iota ( l.begin (), l.end (), 123 );