ostream_iterator and a collection of pointers

Discussion in 'C++' started by ShaunJ, Jun 26, 2009.

  1. ShaunJ

    ShaunJ Guest

    I would like to use an ostream_iterator to print the contents of a
    collection of pointers. I don't want to print the pointers, however,
    but the objects to which they point.

    Thanks,
    Shaun
     
    ShaunJ, Jun 26, 2009
    #1
    1. Advertising

  2. ShaunJ

    Jerry Coffin Guest

    In article <c3bfe697-6e6e-4b8d-9b16-2460796b3480
    @a39g2000pre.googlegroups.com>, says...
    > I would like to use an ostream_iterator to print the contents of a
    > collection of pointers. I don't want to print the pointers, however,
    > but the objects to which they point.


    Exactly as stated, I don't think it's possible. If you're willing to
    use a proxy class for the pointers, it becomes pretty trivial:

    // warning: code only minimally tested.
    #include <iostream>

    template <class T>
    class Proxy {
    T ptr;
    public:
    Proxy(T p) : ptr(p) {}
    operator T() { return ptr; }
    };

    // For this to work 'T' must be a pointer(like) object -- one
    // for which unary '*' is supported.
    template <class T>
    std::eek:stream &operator<<(std::eek:stream &os, Proxy<T> p) {
    return os << *p;
    }

    and a quick demo to show it can do the job:

    #ifdef TEST
    #include <vector>

    int main() {
    std::vector<Proxy<int *> > ints;

    int a = 0, b=1, c=2;

    ints.push_back(&a);
    ints.push_back(&b);
    ints.push_back(&c);

    std::copy(ints.begin(),
    ints.end(),
    std::eek:stream_iterator<Proxy<int *> >(std::cout, "\n"));
    return 0;
    }

    #endif

    I'll leave it to you to decide whether this is really a good idea or
    not. It does what you've asked with essentially no overhead, but I'm
    still not sure I'd use it in production code. At the very least, I'd
    probably rename the Proxy class to something indicating that it's
    intended as a proxy for pointers. With support for concepts, we could
    include (in real code) some assurance that T is dereferencable, but
    as it stands right now, we only have comments to assure that (along
    with the fact that otherwise code using it won't compile, but the
    error messages may be a bit ugly.

    --
    Later,
    Jerry.
     
    Jerry Coffin, Jun 26, 2009
    #2
    1. Advertising

  3. ShaunJ

    Jerry Coffin Guest

    In article <>,
    says...

    [ ... ]

    > // For this to work 'T' must be a pointer(like) object -- one
    > // for which unary '*' is supported.


    Thinking about it a moment longer, in this case we can enforce that
    restriction (without Concepts). We just change operator<< to look
    like:
    template <class T>
    std::eek:stream &operator<<(std::eek:stream &os, Proxy<T *> p) {
    return os << *p;
    }

    --
    Later,
    Jerry.
     
    Jerry Coffin, Jun 26, 2009
    #3
  4. ShaunJ wrote:

    > I would like to use an ostream_iterator to print the contents of a
    > collection of pointers. I don't want to print the pointers, however,
    > but the objects to which they point.


    You could use std::transform to copy the collection to the stream and
    dereference the contents in one go:

    struct Dereference
    {
    template <typename T>
    const T& operator()(const T* item) const
    { return *item; }
    };

    std::transform(coll.begin(), coll.end(),
    std::eek:stream_iterator<T>(stream, ""), Dereference());

    >
    > Thanks,
    > Shaun


    Bart v Ingen Schenau
    --
    a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
    c.l.c FAQ: http://c-faq.com/
    c.l.c++ FAQ: http://www.parashift.com/c -faq-lite/
     
    Bart van Ingen Schenau, Jun 28, 2009
    #4
  5. ShaunJ

    ShaunJ Guest

    On Jun 28, 9:04 am, Bart van Ingen Schenau <>
    wrote:
    > ShaunJ wrote:
    > > I would like to use an ostream_iterator to print the contents of a
    > > collection of pointers. I don't want to print the pointers, however,
    > > but the objects to which they point.

    >
    > You could use std::transform to copy the collection to the stream and
    > dereference the contents in one go:
    >
    > struct Dereference
    > {
    > template <typename T>
    > const T& operator()(const T* item) const
    > { return *item; }
    >
    > };
    >
    > std::transform(coll.begin(), coll.end(),
    > std::eek:stream_iterator<T>(stream, ""), Dereference());


    Fantastic! This is exactly what I was looking for. Thanks,
    Shaun
     
    ShaunJ, Jun 30, 2009
    #5
    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. Chris Mantoulidis
    Replies:
    2
    Views:
    423
  2. Chris Johnson
    Replies:
    6
    Views:
    344
    Chris Johnson
    Jul 3, 2006
  3. Øyvind Isaksen
    Replies:
    1
    Views:
    1,002
    Øyvind Isaksen
    May 18, 2007
  4. , India
    Replies:
    3
    Views:
    4,230
    James Kanze
    Oct 1, 2007
  5. ShaunJ
    Replies:
    7
    Views:
    1,145
    James Kanze
    Jun 16, 2009
Loading...

Share This Page