replace() replace_copy()
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()的更多相关文章
- CString和string
CString和string(一) 概述 string和CString均是字符串模板类,string为标准模板类(STL)定义的字符串类,已经纳入C++标准之中: CString(typedef CS ...
- ACM学习
转:ACM大量习题题库 ACM大量习题题库 现在网上有许多题库,大多是可以在线评测,所以叫做Online Judge.除了USACO是为IOI准备外,其余几乎全部是大学的ACM竞赛题库. US ...
- ACM竞赛常用STL(二)之STL--algorithm
<algorithm>无疑是STL 中最大的一个头文件,它是由一大堆模板函数组成的.下面列举出<algorithm>中的模板函数: adjacent_find / binary ...
- 转:char*, char[] ,CString, string的转换
转:char*, char[] ,CString, string的转换 (一) 概述 string和CString均是字符串模板类,string为标准模板类(STL)定义的字符串类,已经纳入C++标准 ...
- (转载)ACM训练计划,先过一遍基础再按此拼搏吧!!!!
ACM大量习题题库 ACM大量习题题库 现在网上有许多题库,大多是可以在线评测,所以叫做Online Judge.除了USACO是为IOI准备外,其余几乎全部是大学的ACM竞赛题库. USACO ht ...
- 泛型1(一些algorithm函数)
泛型算法本身不会执行容器的操作,它们只会运行于迭代器之上,执行迭代器的操作.因此算法可能改变容器中保存的元素,也可能在容器内移动元素,但永远不会直接添加或删除元素. 只读算法: accumulate: ...
- 另一个ACM之路建议
ACM联系建议 一位高手对我的建议: 一般要做到50行以内的程序不用调试.100行以内的二分钟内调试成功.acm主要是考算法的 ,主要时间是花在思考算法上,不是花在写程序与debug上. 下面给个计划 ...
- 《STL源码剖析》——第五、六:关联容器与算法
第五章.关联容器 5.0.关联容器 标准的STL关联式容器分为set(集合)和map(映射表)两大类,以及这两大类的衍生体multiset(多键集合)和multimap(多键映射表).这些容器的底层 ...
- 算法 replace,replace_copy,back_inserter
replace (list.begin(), list.end(), , ); // replace any elements with value of 0 by 42 replace算法对输入序列 ...
随机推荐
- 百度VS高德:LBS开发平台ios SDK对比评测
随着iPhone6手机的热销,目前的iOS应用开发市场也迎来了全盛时期.据了解,目前市面上已有的iOS应用基本覆盖了购物.上门服务.用车服务.娱乐等行业.而在这些iOS应用中,内置LBS服务的应用占大 ...
- Logwatch的配置与使用
Logwatch是使用 Perl 开发的一个日志分析工具 Logwatch能够对Linux 的日志文件进行分析,并自动发送mail给相关处理人员,可定制需求 Logwatch的mail功能是借助宿主系 ...
- HDU 4054
http://acm.hdu.edu.cn/showproblem.php?pid=4054 模拟题,对一个字符串的每个字符输出16进制表示的数字,每行处理16个字符,后面再把这16个字符输出,大小写 ...
- HDU 3932
http://acm.hdu.edu.cn/showproblem.php?pid=3932 一定范围的平面上给一些点,求到这些点的最大距离最小,和上一题的题意正好相反,稍微改一下就可以 这个问题又叫 ...
- INNO:检测程序是否已经安装,是则弹出卸载提示。
INNO:检测程序是否已经安装,是则弹出卸载提示. 作者:少轻狂 | 发布:2010-08-05 | 更新:2013-09-05 | 分类:部署 | Disposition | 热度:2816 ℃ 实 ...
- cocos2dx 搭建 android 平台 -2
1.配置Cocos2d-x for Xcode. 这一块比较简单, 可以参见其他文章. 重点:install-templates-xcode.sh 2.配置普通Android开发环境 这一块包括JDK ...
- magento -- 如何为商品分类(category)添加自定义属性
在magento 中,由于使用了强大的EAV设计方法,我们可以很方便的给商品添加任意数量的属性.然而magento 没有给我们提供给商品分类添 加属性的功能.尽管我们知道magento所采用的EAV设 ...
- 转:Repeater嵌套绑定Repeater以及内层调用外层数据
<table border=" style="margin-bottom: 5px" width="100%"> <asp:Repe ...
- 前端学习资源(js)
JavaScript JavaScript | MDN JavaScript 秘密花园 JavaScript 标准参考教程(alpha) 给 JavaScript 初心者的 ES2015 实战 Col ...
- [转]C语言文件操作
1,两种文件存取方式(输入,输出方式) 顺序存取 直接存取 2,数据的两种存放形式 文本文件 二进制文件 13.2文件指针 定义文件类型指针变量的一般形式: FILE *指针变量名; 例如: FILE ...