Simple Program interpretation of C ++ STL algorithm series 19th: replace

Source: Internet
Author: User

The replacement algorithm replaces the specified Element value with a new value. The following prototype is used to replace all the elements with the value of old_value in the [first, last) iterator with the value of new_value.

Function prototype:

template < class ForwardIterator, class T >  void replace ( ForwardIterator first, ForwardIterator last,                 const T& old_value, const T& new_value );

Parameter description:

First, last
Specify the iterator range to be replaced [first, last)
Old_value
Element value to be replaced
New_value
New Value of the old value to be replaced

Program example:

/*************************************** * **************************** Copyright (c) jerry Jiang ** file name: replace. CPP * Author: Jerry Jiang * Create Time: 2012-4-29 22:22:18 * mail: jbiaojerry@gmail.com * blog: http://blog.csdn.net/jerryjbiao ** Description: simple program interpretation of C ++ STL algorithm series 19 * variable algorithm: replace ************************************** * ****************************/# include <iostre Am ># include <algorithm> # include <vector> using namespace STD; int main () {int myints [] = {10, 20, 30, 30, 20, 10, 10, 20}; vector <int> myvector (myints, myints + 8); // 10 20 30 20 10 10 20 Replace (myvector. begin (), myvector. end (), 20, 99); // 10 99 30 30 99 10 10 99 cout <"myvector contains:"; for (vector <int> :: iterator it = myvector. begin (); it! = Myvector. End (); ++ it) cout <"" <* it; cout <Endl; return 0 ;}

**************************************** **************************************** **************************************** *******

C ++ classic bibliography index and resource download:Http://blog.csdn.net/jerryjbiao/article/details/7358796

**************************************** **************************************** **************************************** ********

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.