Thread: copy algorithm

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    104

    copy algorithm

    I need help (explanation and examples) in understanding the copy algorithm.
    Code:
    copy(InputIterator sourceBeg, InputIterator sourceEnd, 
             OutputIterator destBeg)
    Can the InputIterators be incremented like
    Code:
    copy(InputIterator sourceBeg+1, InputIterator sourceEnd, 
             OutputIterator destBeg)
    This algorithm only works with/on vectors, correct?
    Would you need to declare your input and output iterators like this:
    Code:
    string::iterator start=source.begin();
    string::iterator end=source.end();
    string::iterator begin=dest.begin();

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    int main()
    {
        vector<int> IntVect;
        for( int j = 0; j < 10; ++j )
            IntVect.push_back(j);
    
        copy( IntVect.begin(), IntVect.end(), ostream_iterator<int>(cout,"\n") );
    
        return 0;
    }
    Outputs via cout:
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9

    You can use this to copy a vector container into a list container for example...
    Code:
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <list>
    using namespace std;
    
    int main()
    {
        vector<int> IntVect;
        list<int> IntList;
    
        for( int j = 0; j < 10; ++j )
            IntVect.push_back(j);
    
        // Copy contents of IntVect into IntList.
            
        copy( IntVect.begin(), IntVect.end(), back_inserter(IntList) );
    
        return 0;
    }
    [edit]Noticed some ';' that I needed to make ','. Sory for any confusion.[/edit]
    Last edited by hk_mp5kpdw; 06-19-2003 at 06:47 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Note the std::back_inserter in
    hk_mp5kpdw's code. That in place of IntList.end() prevents overrunning the bounds of IntList.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    104
    Code:
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    int main()
    {
        vector<int> IntVect;
        for( int j = 0; j < 10; ++j )
            IntVect.push_back(j);
    
        copy( IntVect.begin(), IntVect.end(), ostream_iterator<int>  
               (cout,"\n") );  // I get errors here, see below
    
        return 0;
    }
    When I try this, I get the following errors:
    E2451 undefined symbol 'ostream_interator'
    and,
    E2188 Expression Syntax

    where the copy algorithm is used. see note in code above.
    my compiler is Borland c++ builder 6.
    Last edited by kes103; 06-19-2003 at 08:48 AM.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    148
    Include iterator.
    Code:
    #include <iterator>

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Gcc can't find obvious copy constructor
    By SevenThunders in forum C++ Programming
    Replies: 13
    Last Post: 03-19-2009, 02:41 PM
  2. How to copy a C struct to a C++ class?
    By Ptbamboo in forum C++ Programming
    Replies: 1
    Last Post: 02-21-2009, 02:11 PM
  3. Copying constant amount of data
    By TriKri in forum C++ Programming
    Replies: 16
    Last Post: 07-12-2008, 06:32 AM
  4. Copy constructor question
    By BigFish21 in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2008, 12:18 PM
  5. pointer conversion problems with a copy constructor
    By stanlvw in forum C++ Programming
    Replies: 8
    Last Post: 01-14-2008, 12:06 AM