Sharing a Generic Ostream Iterator

Discussion in 'C++' started by cdiggins@videotron.ca, Nov 11, 2005.

  1. Guest

    The ostream_iterator from the standard library is a template. This
    results in a somewhat inelegant syntax. I thought I would share the
    following alternative ostream_iterator, which avoids the neccessity for
    template instantion

    // public domain code by Christopher Diggins

    #include <iostream>

    struct putter {
    putter(const putter& x) : o(x.o), delim(x.delim) { }
    putter(std::eek:stream& x = std::cout, const char* s = "")
    : o(x), delim(s)
    { }
    template<typename T>
    putter& operator=(const T& x) {
    o << x << delim; return *this;
    }
    putter& operator*() { return *this; }
    putter& operator++() { return *this; }
    putter& operator++(int) { return *this; }
    mutable std::eek:stream& o;
    const char* delim;
    };

    putter put(std::eek:stream& o = std::cout, const char* delim = "") {
    return putter(o, delim);
    }

    usage is as follows:

    int main() {
    int array[] = { 1, 2, 4, 8, 16, 32, 64 };
    std::copy(array, array + 7, put());
    return 0;
    }

    Hope this is useful.

    Christopher Diggins
    http://www.cpp-cookbook.com - C++ Cookbook
    , Nov 11, 2005
    #1
    1. Advertising

  2. Neil Cerutti Guest

    On 2005-11-11, <> wrote:
    > The ostream_iterator from the standard library is a template. This
    > results in a somewhat inelegant syntax. I thought I would share the
    > following alternative ostream_iterator, which avoids the neccessity for
    > template instantion
    >
    > // public domain code by Christopher Diggins
    >
    > #include <iostream>
    >
    > struct putter {
    > putter(const putter& x) : o(x.o), delim(x.delim) { }
    > putter(std::eek:stream& x = std::cout, const char* s = "")
    > : o(x), delim(s)
    > { }
    > template<typename T>
    > putter& operator=(const T& x) {
    > o << x << delim; return *this;
    > }
    > putter& operator*() { return *this; }
    > putter& operator++() { return *this; }
    > putter& operator++(int) { return *this; }
    > mutable std::eek:stream& o;
    > const char* delim;
    > };


    Member o needn't be mutable, need it?

    > putter put(std::eek:stream& o = std::cout, const char* delim = "") {
    > return putter(o, delim);
    > }
    >
    > usage is as follows:
    >
    > int main() {
    > int array[] = { 1, 2, 4, 8, 16, 32, 64 };
    > std::copy(array, array + 7, put());
    > return 0;
    > }
    >
    > Hope this is useful.


    It might not be usable everywhere you need an iterator (it
    provides no type names), but it's short and sweet.

    --
    Neil Cerutti
    Neil Cerutti, Nov 11, 2005
    #2
    1. Advertising

  3. Guest

    , Nov 11, 2005
    #3
  4. wrote:
    > The ostream_iterator from the standard library is a template. This
    > results in a somewhat inelegant syntax. I thought I would share the
    > following alternative ostream_iterator, which avoids the neccessity for
    > template instantion
    >
    > // public domain code by Christopher Diggins
    >
    > #include <iostream>
    >
    > struct putter {
    > putter(const putter& x) : o(x.o), delim(x.delim) { }
    > putter(std::eek:stream& x = std::cout, const char* s = "")
    > : o(x), delim(s)
    > { }
    > template<typename T>
    > putter& operator=(const T& x) {
    > o << x << delim; return *this;
    > }
    > putter& operator*() { return *this; }
    > putter& operator++() { return *this; }
    > putter& operator++(int) { return *this; }
    > mutable std::eek:stream& o;
    > const char* delim;
    > };
    >
    > putter put(std::eek:stream& o = std::cout, const char* delim = "") {
    > return putter(o, delim);
    > }
    >
    > usage is as follows:
    >
    > int main() {
    > int array[] = { 1, 2, 4, 8, 16, 32, 64 };
    > std::copy(array, array + 7, put());
    > return 0;
    > }
    >
    > Hope this is useful.
    >


    I like it! Probably wasn't done this way originally because of the lack
    of support for member function templates back in the old days.

    john
    John Harrison, Nov 11, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hendrik Maryns
    Replies:
    18
    Views:
    1,398
  2. greg
    Replies:
    6
    Views:
    443
    Dietmar Kuehl
    Jul 17, 2003
  3. Noah Roberts
    Replies:
    1
    Views:
    270
    Noah Roberts
    Oct 18, 2007
  4. Replies:
    2
    Views:
    1,881
  5. Gerhard Fiedler

    Generic ostream operator<<?

    Gerhard Fiedler, Jun 19, 2012, in forum: C++
    Replies:
    32
    Views:
    1,388
    Sourav Datta
    Jun 27, 2012
Loading...

Share This Page