Results 1 to 6 of 6

Thread: What is the use of the replace() in C++

  1. #1
    Join Date
    Jan 2010
    Posts
    26

    What is the use of the replace() 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() function of the C++ language. So, I want to know about the replace() function of the C++ language. I also want to know What is the actual use of the replace() function in the C++ Programs. Reply Me!!

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

    The use of the replace() in C++

    Hello, According to my knowledge of the replace() function can be that the replace() function can be used in the C++ language to set all an elements in the given specified range [first,last) whose current value can equals old value to a value of new value. The behavior of the replace() function template can be similar to the following :
    Code:
    template < class FrwrdItrtr, class S >
      void replace ( FrwrdItrtr fst, FrwrdItrtr lst,
                     const S& oldvalue, const S& newvalue )
    {
      for (; fst != lst; ++fst)
        if (*fst == oldvalue) *fst=newvalue;
    }

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

    The use of the replace()

    The program that can be listed below describes you about the replace() function of the C++ language and how you can use the replace() function in the C++ program as :
    Code:
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    int main () 
    {
      int mint[] = { 10, 20, 30, 30, 20, 10, 10, 20 };
      vector<int> mvctr (mint, mint+8);      
      replace (mvctr.begin(), mvctr.end(), 20, 99); 
      cout << "mvctr contains:";
      for (vector<int>::iterator t=mvctr.begin(); t!=mvctr.end(); ++t)
        cout << " " << *t;
      cout << endl;
      return 0;
    }
    Output :
    Code:
    mvctr contains : 10 99 30 30 99 10 10 99

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

    The use of the replace() in C++

    The replace() function can be used in the C++ language to replace the value of the particular an element in the given range. The following can be the general syntax of the replace() function :
    Code:
    template < class FrwrdItrtr, class S >
      void replace ( FrwrdItrtr fst, FrwrdItrtr lst,
                     const S& oldvalue, const S& newvalue );

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

    Program : The replace() function of the 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() function in the C++ language :
    Code:
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    int main ()
     {
      int mya[] = { 10, 20, 30, 30, 20, 10, 10, 20 };
      vector<int> mvct (mya, mya+8);      
      replace (myvctr.begin(), myvctr.end(), 20, 99);
      cout << "myvctr contains:";
      for (vector<int>::aerator a=myvctr.begin(); a!=myvctr.end(); ++a)
        cout << " " << *a;
      cout << endl;
      return 0;
    }
    Output:
    Code:
    myvctr contains: 10 99 30 30 99 10 10 99

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

    Re: What is the use of the replace() in C++

    The following can be the parameters of the replace() function in the C++ language :
    1. newvalue : This parameter of the replace() function can have the replacement value of an element.
    2. oldvalue : This parameter of the replace() function can have the Value that can be replaced.
    3. fst, lst : These parameters of the replace() function can be used for the forward iterators to the final and starting positions in a given sequence. The range used can be as [first,last).

Similar Threads

  1. How do I replace a RAM of Powerbook
    By Cheng-Gong in forum Hardware Peripherals
    Replies: 6
    Last Post: 17-06-2010, 07:07 AM
  2. How to replace a key by another?
    By Caidenn in forum Hardware Peripherals
    Replies: 5
    Last Post: 11-03-2010, 06:59 AM
  3. Replace IMAC DVD
    By Zebaril in forum Hardware Peripherals
    Replies: 3
    Last Post: 07-08-2009, 08:09 AM
  4. How to replace wnaspi32.dll
    By jean-paul martell in forum Windows Software
    Replies: 1
    Last Post: 17-03-2009, 12:04 PM
  5. Should I replace all or buy only 2 strips of RAM ?
    By Elsie in forum Motherboard Processor & RAM
    Replies: 6
    Last Post: 16-03-2009, 12:59 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,751,729,441.49588 seconds with 16 queries