Results 1 to 5 of 5

Thread: Use of the replace_copy() in C++

  1. #1
    Join Date
    Jan 2010
    Posts
    26

    Use of the replace_copy() in C++

    Hi, How are you Everyone. I am the student of the MscIT. I have to submit the Project on the functions of the C++ language in three days. I can have the well knowledge of the functions that can be used in the C++ language. Even if, I can not know much more about the replace_copy() function of the C++ language. So, I want to know about the replace_copy() function of the C++ language. I also want to know How the replace_copy() function can be used in the C++ Programs. Reply Me!!

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

    Use of the replace_copy() in C++

    The replace_copy() function can be used in the C++ language to Copy range replacing value. The following can be the general syntax of the replace_copy() function :
    Code:
    template < class IptItrtr, class OtptItrtr, class T >
      OtptItrtr replace_copy ( IptItrtr first, IptItrtr last,
                                    OtptItrtr result,
                                    const T& old_value, const T& new_value );

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

    Re: Use of the replace_copy() in C++

    I suggest you to carefully go through the following code of lines that can be helpful for you to get the information or knowledge of the replace_copy() function in the C++ language :
    Code:
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    int main ()
     {
      int mint[] = { 10, 20, 30, 30, 20, 10, 10, 20 };
      vector<int> myvctr (8);
      replace_copy (mint, mint+8, myvctr.begin(), 20, 99);
      cout << "myvctr contains:";
      for (vector<int>::iterator it=myvctr.begin(); it!=myvctr.end(); ++it)
        cout << " " << *it;
      cout << endl;
      return 0;
    }
    Output:
    Code:
    myvctr contains: 10 99 30 30 99 10 10 99

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

    The replace_copy() in C++

    The following can be the parameters of the replace_copy() function in the C++ language :
    1. result : This parameter of the replace_copy() function can be used for Output iterator to the starting position of the range where the function's results can be stored. The range can includes as many an elements as [fst,lst).
    2. new_value : This parameter of the replace_copy() function can has the replacement value.
    3. fst, lst : These parameters of the replace_copy() function can be used for Input iterators to the final and starting positions in a given sequence. The range used can be as [first,last)
    4. old_value : This parameter of the replace_copy() function can has the Value that can be replaced.

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

    Re: Use of the replace_copy() in C++

    Program :
    Code:
    #include <iostream>
    #include <fstream>
    #include <iterator>
    #include <algorithm>
    using namespace std;
    int main(int arg, char *ag[])
    {
      if(arg != 5) 
    {
        cout << "Usage: replace in out oldchar newchar\n";
        return 1;
      }
      ifstream in(ag[1]);
      ofstream out(ag[2]);
      if(!in.isopen()) 
    {
        cout << "Cannot open input file.\n";
        return 1;
      }
      if(!out.isopen()) 
    {
        cout << "Cannot open output file.\n";
        return 1;
      }
      istreambufiterator<char> in_itr(in);
      ostreambufiterator<char> out_itr(out);
      replace_copy(in_itr, istreambuf_iterator<char>(),out_itr, *ag[3], *ag[4]);
      in.close();
      out.close();
      return 0;
    }

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,022,167.02478 seconds with 15 queries