int a[] = {1,2,3,3,4};
vector<int> v(a, a+5);
vector<int> v2;

//replace(v.begin(), v.end(), 3, 9);  //把 v 中的3 替换为 9
replace_copy(v.begin(), v.end(), back_inserter(v2), 3, 9);  //把 v 中的3 替换为 9 赋值给v2 ,v的值不变
for (int i =0; i<v.size(); i++)
{
cout<<v[i]<<endl;
}

for (int i=0; i<v2.size(); i++)
{
cout<<v2[i]<<endl;
}

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

int ia[] = { 1, 2, 3, 4, 100, 5, 100 };
vector< int > iVec( ia, ia+7 );

cout << "\n\tThe contents of iVec: ";

for ( vector<int>::iterator it = iVec.begin(); it != iVec.end(); ++it )
{
cout << *it << " ";
}

cout << endl;

list<int> iLst;
// copy iVec's member to iLst;
cout << "\n Using inserter: " << endl;

replace_copy( iVec.begin(), iVec.end(), inserter( iLst, iLst.begin() ), 100, 0 );
cout << "\tThe contents of iLst: ";
for ( list<int>::iterator it = iLst.begin(); it != iLst.end(); ++it )
{
cout << *it << " ";
}
cout << endl;

cout << "\n Using back_inserter: " << endl;
iLst.clear();
replace_copy( iVec.begin(), iVec.end(), back_inserter( iLst ), 100, 0 );
cout << "\tThe contents of iLst: ";

for ( list<int>::iterator it = iLst.begin(); it != iLst.end(); ++it )
{
cout << *it << " ";
}
cout << endl;

cout << "\n Using front_inserter: " << endl;
iLst.clear();
replace_copy( iVec.begin(), iVec.end(), front_inserter( iLst ), 100, 0 );
cout << "\tThe contents of iLst: ";
for ( list<int>::iterator it = iLst.begin(); it != iLst.end(); ++it )
{
cout << *it << " ";
}
cout<<endl;

replace(iLst.begin(), iLst.end(), 0 , 8);

for ( list<int>::iterator it = iLst.begin(); it != iLst.end(); ++it )
{
cout << *it << " ";
}

cout << endl;
system("pause");

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

原文:http://wenwen.soso.com/z/q150702174.htm

replace() replace_copy()的更多相关文章

  1. CString和string

    CString和string(一) 概述 string和CString均是字符串模板类,string为标准模板类(STL)定义的字符串类,已经纳入C++标准之中: CString(typedef CS ...

  2. ACM学习

    转:ACM大量习题题库   ACM大量习题题库 现在网上有许多题库,大多是可以在线评测,所以叫做Online Judge.除了USACO是为IOI准备外,其余几乎全部是大学的ACM竞赛题库.   US ...

  3. ACM竞赛常用STL(二)之STL--algorithm

    <algorithm>无疑是STL 中最大的一个头文件,它是由一大堆模板函数组成的.下面列举出<algorithm>中的模板函数: adjacent_find / binary ...

  4. 转:char*, char[] ,CString, string的转换

    转:char*, char[] ,CString, string的转换 (一) 概述 string和CString均是字符串模板类,string为标准模板类(STL)定义的字符串类,已经纳入C++标准 ...

  5. (转载)ACM训练计划,先过一遍基础再按此拼搏吧!!!!

    ACM大量习题题库 ACM大量习题题库 现在网上有许多题库,大多是可以在线评测,所以叫做Online Judge.除了USACO是为IOI准备外,其余几乎全部是大学的ACM竞赛题库. USACO ht ...

  6. 泛型1(一些algorithm函数)

    泛型算法本身不会执行容器的操作,它们只会运行于迭代器之上,执行迭代器的操作.因此算法可能改变容器中保存的元素,也可能在容器内移动元素,但永远不会直接添加或删除元素. 只读算法: accumulate: ...

  7. 另一个ACM之路建议

    ACM联系建议 一位高手对我的建议: 一般要做到50行以内的程序不用调试.100行以内的二分钟内调试成功.acm主要是考算法的 ,主要时间是花在思考算法上,不是花在写程序与debug上. 下面给个计划 ...

  8. 《STL源码剖析》——第五、六:关联容器与算法

    第五章.关联容器  5.0.关联容器 标准的STL关联式容器分为set(集合)和map(映射表)两大类,以及这两大类的衍生体multiset(多键集合)和multimap(多键映射表).这些容器的底层 ...

  9. 算法 replace,replace_copy,back_inserter

    replace (list.begin(), list.end(), , ); // replace any elements with value of 0 by 42 replace算法对输入序列 ...

随机推荐

  1. UNIX,基础知识,文件IO,文件和目录

    2015.1.27星期二,早晨阴天,中午下雪了今天上午老师不上课,程序语句,记一下:main(void){ int c; while((c = getc(stdin)) != EOF) if(putc ...

  2. Spring反射机制

    Spring是分层的Java SE/EE应用一站式的轻量级开源框架,以IoC(Inverse of Control)和AOP(Aspect Oriented Programming)为内核,提供了展现 ...

  3. Postfix之mail.cf

    1.# 2. 3.vi /etc/postfix/main.cf #vi编辑postfix配置文件 4.#找到如下配置项酌情修改 5.###### 6.myhostname =  mail.yourd ...

  4. BZOJ3028 食物 (生成函数)

    首先 1+x+x^2+x^3+...+x^∞=1/(1-x) 对于题目中的几种食物写出生成函数 (对于a*x^b , a表示方案数 x表示食物,b表示该种食物的个数) f(1)=1+x^2+x^4+. ...

  5. 著名的安装制作软件InnoSetup的源码及示例源码-The installation of a well-known software s source code and sample InnoSetup source

    @echo off rem Inno Setup rem Copyright (C) 1997-2007 Jordan Russell rem Portions by Martijn Laan rem ...

  6. url重写技术

    URL 重写是截取传入 Web 请求并自动将请求重定向到其他 URL 的过程.比如浏览器发来请求 hostname/101.aspx ,服务器自动将这个请求中定向为http://hostname/li ...

  7. NSFileHandle 、 沙箱机制 、 属性列表

    1 使用NSFilehandle进行数据读写 1.1 问题 NSFileManager用于实现对文件的操作,而NSFileHandle是IOS提供的对文件内容(二进制数据)进行操作的类,例如数据的读写 ...

  8. 正则表达式 regular expression

    原博客地址:http://www.cnblogs.com/deerchao/archive/2006/08/24/zhengzhe30fengzhongjiaocheng.html

  9. Python漫谈-比较运算符和类的神奇方法

    昨天遇到一个Python问题,今天好奇试了一下 >>> a = {1:23,'ab':56} >>> b = {2:22,'ab':57} >>> ...

  10. JAVA 打印指定月份日历

    package learnExercise; import java.util.Scanner; public class PrintCalender { /** * @param args */ p ...