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算法对输入序列 ...
随机推荐
- JQuery源码分析(三)
jQuery中ready与load事件 jQuery有3种针对文档加载的方法 $(document).ready(function() { // ...代码... }) //document read ...
- Cocos2d-x 3.x项目创建
1.首先打开终端,cd到cocos2d-x-3.2目录下,运行命令./setup.py 2. 首先,打开终端cd到目录/cocos2d-x-3.2/tools/cocos2d-console/bin下 ...
- Mac快捷键 Xcode快捷键
Mac OSX 快捷键&命令行 一.Mac OSX 快捷键 ctrl+shift 快速放大dock的图标会暂时放大,而 ...
- Difference Between Performance Testing, Load Testing and Stress Testing
http://www.softwaretestinghelp.com/what-is-performance-testing-load-testing-stress-testing/ Differen ...
- OpenFlow Switch学习笔记(四)——Matching
这次我们着重详述来自于网络中的数据包在OpenFlow Switch中与Flow Entries的具体匹配过程,以及当出现Table Miss时的处理方式,下面就将从这两方面说起. 1.Matchin ...
- 关于java发起http请求
我们到底能走多远系列(41) 扯淡: 好久没总结点东西了,技术上没什么总结,感觉做事空牢牢的.最近也比较疲惫. 分享些东西,造福全人类~ 主题: 1,java模拟发起一个http请求 使用HttpUR ...
- 解决load 函数无法赋予变量名的问题
以前非常喜欢使用load函数,因为简单,而且存储相对较大的matrix.list文件更为方便.但是load函数有一个问题是在使用其过程中无法对其载入的data赋予变量名: # save data x ...
- 如何为libs目录下的jar包关联源代码
以前,我们可以为lib目录下的jar包关联源代码,但是现在似乎不行了. 下面是一篇讲述此问题解决方法的文章: How to attach javadoc or sources to jars in l ...
- HDFS简介【全面讲解】
http://www.cnblogs.com/chinacloud/archive/2010/12/03/1895369.html [一]HDFS简介HDFS的基本概念1.1.数据块(block)HD ...
- Java-->把txt中的所有字符按照码表值排序
--> List 列表中的自动添加的多余空间长度该怎么去除呢?...(已解决,是char 数组中的空字符) package com.dragon.java.filesort; import ja ...