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算法对输入序列 ...
随机推荐
- codeforces #Round354-div2-D(BFS)
题目链接:题目链接 题意:一个n*m的区域,每个格子都有上下左右四个门,相邻的两个格子A可以通向B当且仅当A对B的门和B对A的门都打开,问从起点S到终点T需要的最短时间 #include<bit ...
- Send Mail using C# code
using System.Net.Mail; public class MailHelp { public static void Send(string subject, string body) ...
- session过期时ajax请求刷新浏览器
ajax前置处理实现异步请求session过期时跳转登录页面 function checkLogin(json) { if (typeof(json) === 'string' && ...
- 关于cocos2d-x 与 cocos2d-html5 资源预加载的思考
移动端资源预加载,可以做到需要加载的时候,从本地磁盘加载到内存,当纹理不需要的时候,都是强制清理内存里的纹理占用: cc.TextureCache.getInstance().removeAllTex ...
- IE6无法加载CSS
问题:写了个页面,IE7和IE8下正常,但发现IE6下竟然没有加载css样式. 找原因,发现是因为CSS文件的编码不正确,页面是采用utf-8编码的,但样式表是复制过来的,编码是ANSI,这种情况下在 ...
- SPAdes
用后感: 拼个小基因组还好,对于很大的基因组,文库很多的,还是不要用了.服务器768G内存,都不够用.... 主页: http://bioinf.spbau.ru/spades 说明书: http:/ ...
- Git简易的命令行入门教程
简易的命令行入门教程: Git 全局设置: git config --global user.name "imsoft" git config --global user.emai ...
- VMware中三种网络连接的区别
1.概述 大家在安装完虚拟机后,默认安装了如下图的两块虚拟网卡——VMnet1和VMnet8,其中VMnet1是host网卡,用于host方式连接网络:VMnet8是NAT网卡,用于NAT方式连接网络 ...
- tyvj 1057 dp 变形背包
P1057 金明的预算方案 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 NOIP2006 提高组 第二道 描述 金明今天很开心,家里购置的新房就要领钥匙了 ...
- 《C标准库》—之<assert.h>实现
首先,贴出标准库中<assert.h>的实现源码: #undef assert #ifdef NDEBUG #define assert(test)((void)0) #else void ...