Results 1 to 6 of 6

Thread: Use the copy() in CPP

  1. #1
    Join Date
    Dec 2009
    Posts
    22

    Use the copy() in CPP

    Hi, I am the student of the BscIT. I have to make the project on the C++ language. But I can not know anything about the copy() function of the C++ language. So, I would like to know about the copy() function in C++ language. I also would like to know how can I use the copy() function in the C++ program code. Thus, Can anyone help me to know about the copy() function.
    Last edited by Garett; 16-02-2010 at 08:40 PM.

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    The copy() in CPP

    The following program demonstrates you about the copy() function in the C++ language :
    Code:
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    int main ()
     {
      int mint[]={10,20,30,40,50,60,70};
      vector<int> mvctr;
      vector<int>::iterator t;
      mvctr.resize(7);  
      copy ( mint, mint+7, mvctr.begin() );
      cout << "mvctr contains:";
      for (t=mvctr.begin(); t!=mvctr.end(); ++t)
        cout << " " << *t;
      cout << endl;
      return 0;
    }
    Output:
    Code:
    myvector contains: 10 20 30 40 50 60 70

  3. #3
    Join Date
    Apr 2008
    Posts
    1,948

    copy() in CPP

    PROGRAM :
    Code:
    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <iterator>
    #include <functional>
    #include <numeric>
    using namespace std;
    template <class S>
    inline void PRINTELEMENTS (const S& cl, const char* optstr="")
    {
        typename S::constiterator ps;
        std::cout << optstr;
        for (ps=cl.begin(); ps!=cl.end(); ++ps) 
    {
            std::cout << *ps << ' ';
        }
        std::cout << std::endl;
    }
    template <class S>
    inline void INSERTELEMENTS (S& cl, int fst, int lst)
    {
        for (int j=fst; j<=lst; ++j)
     {
            cl.insert(cl.end(),j);
        }
    }
    int main()
    {
        vector<int> cl1;
       list<int> cl2;
        INSERTELEMENTS(cl1,1,9);
        copy (cl1.begin(), cl1.end(),        
              backinserter(cl2));              
        copy (cl2.begin(), cl2.end(),      
              ostreamiterator<int>(cout," "));
        cout << endl;
    }
    OUTPUT :
    Code:
    1 2 3 4 5 6 7 8 9

  4. #4
    Join Date
    May 2008
    Posts
    2,012

    Use the copy() in CPP

    The copy() function can be used to copy the elements in the given range [first,last) into a range that can be beginning at result. The behavior of this function template can be similar to :
    Code:
    template<class IptItrtr, class OutputIptItrtr>
      OutputIptItrtr copy ( InputIptItrtr first, InputIptItrtr last, 
    OutputIptItrtr result )
    {
      while (fst!=lst) *rslt++ = *fst++;
      return rslt;
    }

  5. #5
    Join Date
    Apr 2008
    Posts
    2,005

    Use the copy() in CPP

    The program that can describes the copy() function in the C++ language :
    Code:
    #include <vector>
    #include <algorithm>
    #include <iostream>
    int main() 
    {
       using namespace std;
       vector <int> w1, w2;
       vector <int>::iterator Itr1, Itr2;
       int j;
       for ( j = 0 ; j <= 5 ; j++ )
          w1.pushback( 10 * j );
       int jj;
       for ( jj = 0 ; jj <= 10 ; jj++ )
          w2.pushback( 3 * jj );
       cout << "w1 = ( " ;
       for ( Itr1 = w1.begin( ) ; Itr1 != w1.end( ) ; Itr1++ )
          cout << *Itr1 << " ";
       cout << ")" << endl;
       cout << "w2 = ( " ;
       for ( Itr2 = w2.begin( ) ; Itr2 != w2.end( ) ; Itr2++ )
          cout << *Itr2 << " ";
       cout << ")" << endl;
       copy( w1.begin( ), w1.begin( ) + 3, w2.begin( ) + 4 );
       cout << "w2 with w1 insert = ( " ;
       for ( Itr2 = w2.begin( ) ; Itr2 != w2.end( ) ; Itr2++ )
          cout << *Itr2 << " ";
       cout << ")" << endl;
       copy( w2.begin( )+4, w2.begin( ) + 7, w2.begin( ) + 2 );
       cout << "w2 with shifted insert = ( " ;
       for ( Itr2 = w2.begin( ) ; Itr2 != w2.end( ) ; Itr2++ )
          cout << *Itr2 << " ";
       cout << ")" << endl;
    }
    Output :
    Code:
    w1 = ( 0 10 20 30 40 50 )
    w2 = ( 0 3 6 9 12 15 18 21 24 27 30 )
    w2 with w1 insert = ( 0 3 6 9 0 10 20 21 24 27 30 )
    w2 with shifted insert = ( 0 3 0 10 20 10 20 21 24 27 30 )

  6. #6
    Join Date
    May 2008
    Posts
    2,297

    Re: Use the copy() in CPP

    The following can be the parameters of the copy() function in the C++ language :
    1. rslt : This parameter can be used for the output iterator that can be to the starting position in the sequence that can be the destination.
    2. fst, lst : These parameters can be used for the input iterators to the final and starting positions in a sequence that can be copied.

Similar Threads

  1. Replies: 4
    Last Post: 22-12-2011, 05:45 PM
  2. How to copy DVD on Mac OS X
    By ApplePowerPC in forum Operating Systems
    Replies: 4
    Last Post: 04-02-2010, 03:07 AM
  3. Replies: 0
    Last Post: 18-03-2009, 10:00 PM
  4. How to copy Copy emoticons from Contacts in MSN Messenger 7
    By ChrisUlrich in forum Tips & Tweaks
    Replies: 1
    Last Post: 18-03-2009, 07:11 PM
  5. Copy protected CD need to copy some songs
    By SagarP in forum Windows Software
    Replies: 6
    Last Post: 11-09-2008, 01:56 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,116,479.58286 seconds with 17 queries