Topic: Missing functionality


Author: musiphil@bawi.org (Seungbeom Kim)
Date: Tue, 11 Jul 2006 21:13:25 GMT
Raw View
Aaron Graham wrote:
> Why isn't there a std::copy_n function in the C++ standard?  Someone
> asked this question 7 years ago on this newsgroup but nobody responded.

What about std::copy_if? From what Stroustrup mentioned in The C++
Programming Language (3rd ed), dropping it from the standard seems
to have been a mere mistake. I feel it is very useful (far more than
std::copy_n) but I don't see it in the latest draft (N2009) yet.

Has there been any proposal about introducing std::copy_if? (I don't
see any in the Library Issues List, either.)

--
Seungbeom Kim

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Esko" <esko.ruoko@hotmail.com>
Date: Sun, 2 Jul 2006 18:49:59 CST
Raw View
I would also like to see copy_n in the standard. However, I have found
copy_n to be very error prone with iostream iterators. In fact, all the
available implementations are somehow dangerous.

One could assume that the code example Ex1 would produce same output as
the standard copy does but it does not. The correct way to achieve
identical output is shown in the Ex2. Unfortunately, real advantage for
using copy_n to read data from input stream would be something similar
that Ex3 tries to do. However, using that in your code would really
ruin your day!

Here is an old posting about the copy_n to stlport forum if someone
happens to be interested to create proper implementation for copy_n.

http://www.stlport.com/dcforum/DCForumID6/1966.html



#include <sstream>   // for istringstream
#include <algorithm> // for copy_n
#include <iterator>  // for istream_iterator and ostream_iterator
#include <iostream>  // for cout
#include <string>    // for string

using namespace std;

template <class _InputIter, class _Size, class _OutputIter>
pair<_InputIter, _OutputIter> copy_n(_InputIter __first, _Size __count,
_OutputIter __result) {
    for ( ; __count > 0; --__count) {
        *__result = *__first;
        ++__first;
        ++__result;
    }
    return pair<_InputIter, _OutputIter>(__first, __result);
}

int main()
{
    // Ex1

    istringstream ex1("a b c d e f");
    copy_n(istream_iterator<char>(ex1),2,
        ostream_iterator<char>(cout, " "));
    copy_n(istream_iterator<char>(ex1),2,
        ostream_iterator<char>(cout, " "));
    copy_n(istream_iterator<char>(ex1),2,
        ostream_iterator<char>(cout, " "));
    cout << '\n';

    // Ex2

    istringstream ex2("a b c d e f");
    istream_iterator<char> itr2 = istream_iterator<char>(ex2);
    itr2 = copy_n(itr2,2,ostream_iterator<char>(cout, " ")).first;
    itr2 = copy_n(itr2,2,ostream_iterator<char>(cout, " ")).first;
    itr2 = copy_n(itr2,2,ostream_iterator<char>(cout, " ")).first;
    cout << '\n';
    // Ex3

    istringstream ex3("11 22 AA BB 33 44 CC DD");
    copy_n(istream_iterator<int>(ex3),2,
        ostream_iterator<int>(cout, " "));
    copy_n(istream_iterator<string>(ex3),2,
        ostream_iterator<string>(cout, " "));
    copy_n(istream_iterator<int>(ex3),2,
        ostream_iterator<int>(cout, " "));
    copy_n(istream_iterator<string>(ex3),2,
        ostream_iterator<string>(cout, " "));
    cout << '\n';

    return 0;
}

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Aaron Graham" <atgraham@gmail.com>
Date: Wed, 28 Jun 2006 12:38:51 CST
Raw View
Why isn't there a std::copy_n function in the C++ standard?  Someone
asked this question 7 years ago on this newsgroup but nobody responded.

I often find myself in situations where this functionality would be
beneficial and would make my code more concise and easier to maintain.
To workaround it, I either end up writing copy_n myself, using memcpy,
or defining extra variables depending on the situation; alternatives I
don't find particularly attractive.  Most of the C++ libraries I work
with have copy_n available as an extension, but I don't use it because
my job requires me to write portable code.

Should I make a proposal for TR2, or has this already been done at some
time in the past?  I haven't been able to find any mention of copy_n at
the WG21 website.

Aaron

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]