简单的程序诠释C++ STL算法系列之十九:replace

    替换算法将指定元素值替换为新值,使用原型如下,将迭代器[first,last)中值为old_value的元素全部替换为new_value值。

    函数原型:

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

     参数说明:

first, last
指出要替换的迭代器区间[first,last)
old_value
将要被替换的元素值
new_value
将要替换旧值的新值

     程序示例:

/*******************************************************************  
 * 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 : 简单的程序诠释C++ STL算法系列之十九                    
 *               变易算法 : 替代 replace 
 *                 
 ******************************************************************/    
#include <iostream>
#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 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++经典书目索引及资源下载:http://blog.csdn.net/jerryjbiao/article/details/7358796

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

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值