Thread: Objects with ostream_iterator and copy()

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    55

    Objects with ostream_iterator and copy()

    if I want to do this
    Code:
    std::copy(myobj.begin(),myobj.end(),std::ostream_iterator<myobjtype>(std::cout, "\n");
    What method do I have to overload in myobjtype to get this to work? Is it operator<<()?

    Code:
    ostream & operator<<(ostream &, myobjtype);
    Is that the right signature?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You need to implement a forward iterator concept for you class.
    And you need to implement << operator, yes.
    The signature should be
    std::ostream & operator << (std::ostream & lhs, const myobjtype & rhs);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes, you'll need to overload that operator. If the object is large, you'd rather pass it by const reference.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    55
    Thank you. Elysia, what else needs to be done than just implement operator<<()?

    Edit:
    I think I know what you mean now. myobj is a std::vector. I realize now that that makes my code example a little off.
    The actual sample is:
    Code:
        std::vector<move> * moves = get_legal_moves();
        std::copy(moves->begin(), moves->end,
                std::ostream_iterator<move>(std::cout, "\n"));
    Last edited by Nextstopearth; 10-24-2010 at 02:55 PM.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Forward iterator and operator << should be all.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    >>Forward iterator
    vector::iterator should work... move is not a container class.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This new code certainly doesn't require an iterator concept. But this was not there when I made the original reply.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-15-2010, 07:53 PM
  2. Objects as attributes of other objects.
    By kbro3 in forum C++ Programming
    Replies: 10
    Last Post: 08-15-2009, 03:46 PM
  3. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  4. chain of objects within pop framework help needed
    By Davey in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2004, 10:01 AM
  5. array of objects?
    By *~*~*~* in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 05:57 PM