Thread: Return Type of std::copy()

  1. #1
    Registered User R41D3N's Avatar
    Join Date
    Mar 2012
    Location
    ...Beyond Infinity, Behind a KeyBoard...
    Posts
    29

    Return Type of std::copy()

    I was just wondering why std::copy() returns an OutputIterator.

    Code:
    template<class InputIterator, class OutputIterator>
    
    
    OutputIterator copy ( InputIterator first, InputIterator last, OutputIterator result )
    {
        while (first!=last) 
            *result++ = *first++;
        
        return result;
    }



    when it could have returned void, which works in almost the same way. Is there any valid usage of the iterator returned by copy() ?
    -- reserved for when I come up with 1 --

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    >> Is there any valid usage of the iterator returned by copy() ?

    Yes. I'm tempted to say use your imagination.

    What if you wanted to copy two data structures into one, with only two copy calls? Suddenly the return value is useful.

  3. #3
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Well, I guess it's useful in cases where you need the next position at which you can add stuff after the copying.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    when it could have returned void, which works in almost the same way.
    O_o

    Do have any idea how foolish "Returning `void' works almost the same way as returning `iterator'!' sounds?

    It could have returned no other thing and been as useful.

    Is there any valid usage of the iterator returned by copy()?
    If you don't even know why the iterator is returned then why are you saying it should have just returned `void'?

    Of course there is a valid usage. The input can constitute and unknown range and the output can reference an unknown iterator into an unknown data structure. Knowing the iterator to the "next item" is the only way of propagating the information about the consumed and generated range without counting the elements within that range which is as natural to propagating that range as breathing.

    *shrug*

    I know you are a newbie, but in the future ask your questions about things you don't know without the fluff of commenting on things you don't understand.

    Soma

  5. #5
    Registered User R41D3N's Avatar
    Join Date
    Mar 2012
    Location
    ...Beyond Infinity, Behind a KeyBoard...
    Posts
    29
    Quote Originally Posted by whiteflags View Post
    >> Is there any valid usage of the iterator returned by copy() ?

    Yes. I'm tempted to say use your imagination.

    What if you wanted to copy two data structures into one, with only two copy calls? Suddenly the return value is useful.

    couldnt it be done somewhat like this:

    Code:
    copy(X1.begin(), X1.end(), inserter(X3, X3.begin()));
    
    
    copy(X2.begin(), X2.end(), inserter(X3, X3.end()));
    Do correct me if am wrong .

    Quote Originally Posted by phantomotap View Post

    I know you are a newbie, but in the future ask your questions about things you don't know without the fluff of commenting on things you don't understand.
    I just thought of it as clarifying as to why I was asking the question in the first place. I apologize for it
    -- reserved for when I come up with 1 --

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    @R41D3N: Yes, you can do it that way, but for the purposes of my example, you can't do it that way. That's how I feel about it. Nya nya!

  7. #7
    Registered User R41D3N's Avatar
    Join Date
    Mar 2012
    Location
    ...Beyond Infinity, Behind a KeyBoard...
    Posts
    29
    @whiteflags: thought so ... back to some brain storming
    -- reserved for when I come up with 1 --

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. return type
    By technosavvy in forum C Programming
    Replies: 3
    Last Post: 07-01-2008, 01:05 AM
  2. copy LPCSTR type
    By Gordon in forum Windows Programming
    Replies: 3
    Last Post: 10-17-2007, 09:33 PM
  3. Replies: 6
    Last Post: 04-09-2006, 04:32 PM
  4. copy constructor and return type problem
    By waxydock in forum C++ Programming
    Replies: 3
    Last Post: 04-30-2005, 01:21 AM
  5. copy constructor/type converter?
    By kny in forum C++ Programming
    Replies: 2
    Last Post: 03-13-2002, 10:44 PM

Tags for this Thread