Results 1 to 6 of 6

Thread: Implement the copy_backward() in C++

  1. #1
    Join Date
    Dec 2009
    Posts
    33

    Implement the copy_backward() in C++

    Hello, I am the student of the MscIT. I have learned the C++ language in the BscIT. But I can not know about the Header file functions. So, I would like to know about the copy_backward() function. I aslo would like to know about how can I able to implement the copy_backward() function in the C++ program and what can be the use of the copy_backward() function in the C++ language. Can anyone help me???
    Last edited by Agustíne; 16-02-2010 at 09:31 PM.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Implement the copy_backward() in C++

    The copy_backward() function can be used to Copy an elements in the given range [first,last) into a range whose element which is last can be the result. the following can be the syntax of the copy_backward() function as :
    Code:
    template <class BidirectionalItrtr1, class BidirectionalItrtr2>
      BidirectionalItrtr2 copy_backward ( BidirectionalItrtr1 first,
                                             BidirectionalItrtr1 last,
                                             BidirectionalItrtr2 result );

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Implement the copy_backward()

    The following code of lines demonstrates you that how can you implement the copy_backward() function in the C++ language :
    Code:
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    int main ()
     {
      vector<int> mvcto;
      vector<int>::iterator t;
      for (int j=1; j<=5; j++)
        mvctr.pushback(j*10);    
      mvctr.resize(mvctr.size()+3); 
      copybackward ( mvctr.begin(), mvctr.begin()+5, mvctr.end() );
      cout << "mvctr contains:";
      for (t=mvctr.begin(); t!=mvctr.end(); ++t)
        cout << " " << *t;
      cout << endl;
      return 0;
    }
    Output:
    Code:
    mvctr contains: 10 20 30 10 20 30 40 50

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

    copy_backward() in C++

    Program :
    Code:
        #include <algorithm>   
          #include <iostream>    
          #include <vector>      
          int main ()
          {
              typedef std::vector<short, std::allocator<short> > vector;
              const vector::valuetype e1[] = { 1, 2, 3, 4 };
              const vector::valuetype e2[] = { 5, 6, 7, 8 };
              vector w1(e1 + 0, e1 + 4), 
                     w2(e2 + 0, e2 + 4), 
                     w3(e2 + 0, e2 + 4);
              vector w4;
              std::copy(w1.begin(), w1.end(), w2.begin());
              std::copybackward(w1.begin(), w1.end(), w3.end());
              std::copy(w1.begin(), w1.end(), std::backinserter(w4));
              std::ostreamiterator<vector::valuetype, char,
              std::chartraits<char> > out(std::cout, " ");
              std::copy(w1.begin(), w1.end(), out);
              std::cout << std::endl;
              std::copy(w2.begin(), w2.end(), out);
              std::cout << std::endl;
              std::copy(w3.begin(), w3.end(), out);
              std::cout << std::endl;
              std::copy(w4.begin(), w4.end(), out);
              std::cout << std::endl;
              return 0;
          }
    Output :
    Code:
          1 2 3 4 
          1 2 3 4 
          1 2 3 4 
          1 2 3 4

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Implement the copy_backward() in C++

    There can be one more example on the copy_backward() function in C++ language :
    Code:
    #include <iostream>
    #include <cassert>
    #include <algorithm>
    #include <vector>
    #include <string>
    #include <iostream>
    using namespace std;
    int main()
    {
      string t("abcdefghihklmnopqrstuvwxyz");
      vector<char> vctr1(t.begin(), t.end());
      copy_backward(vctr1.begin(), vctr1.end() - 2, vctr1.end());
      vector<char>::iterator ps;
      for (ps=vctr1.begin(); ps!=vctr1.end(); ++ps)
     {
            cout << *ps << ' ';
      }
      return 0;
    }
    OUTPUT :
    Code:
    a b a b c d e f g h i h k l m n o p q r s t u v w x

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

    copy_backward()

    The copy_backward() function can begins by copying *(lst-1) into *(rslt-1), and after that the copy_backward() function can follows the backwards by an elements that can be preceding these, until first can be reached.
    The behavior of the copy_backward() function template can be similar to the following :
    Code:
    template<class BdrctnlItrtr1, class BdrctnlItrtr2>
      BdrctnlItrtr2 copy_backward ( BdrctnlItrtr1 fst,
                                             BdrctnlItrtr1 lst,
                                             BdrctnlItrtr2 rslt )
    {
      while (lst!=fst) *(--rslt) = *(--lst);
      return rslt;
    }

Similar Threads

  1. How to implement sum() faster in c#?
    By Luis-Fernando in forum Software Development
    Replies: 4
    Last Post: 27-02-2010, 09:25 PM
  2. How can I implement the remove() in CPP
    By Servant in forum Software Development
    Replies: 5
    Last Post: 22-02-2010, 04:18 PM
  3. Implement the equal_range() in C++
    By Garrick in forum Software Development
    Replies: 5
    Last Post: 15-02-2010, 07:09 PM
  4. How to implement IP Sec
    By Enriquee in forum Technology & Internet
    Replies: 5
    Last Post: 12-01-2010, 06:28 AM
  5. Want to implement differentiation in C++
    By ADEN in forum Software Development
    Replies: 1
    Last Post: 25-10-2008, 06:13 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,713,576,513.40234 seconds with 17 queries