C++ replace replace_if replace_copy replace_copy_if
#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>
#include <functional>
using namespace std;
int main()
{
list<int> list1;
list<int> list2;
for (int k=0;k<10;k++)
{
list1.push_back(k);
}
list<int>::iterator list_iter1;
for (list_iter1 = list1.begin(); list_iter1 != list1.end(); ++ list_iter1)
{
cout << *list_iter1 << " ";
}
cout << endl;
cout << "-----------------------------------------------------" << endl;
replace(list1.begin(), list1.end(), 6, 42);
for (list_iter1 = list1.begin(); list_iter1 != list1.end(); ++list_iter1)
{
cout << *list_iter1 << " ";
}
cout << endl;
cout << "-----------------------------------------------------" << endl;
replace_if(list1.begin(), list1.end(), bind2nd(less<int>(), 5), 11);
for (list_iter1 = list1.begin(); list_iter1 != list1.end(); ++list_iter1)
{
cout << *list_iter1 << " ";
}
cout << endl;
cout << "-----------------------------------------------------" << endl;
for (int k=0;k<7;k++)
{
list2.push_back(k);
}
for (int k = 4; k<10; k++)
{
list2.push_back(k);
}
list<int>::iterator list_iter2;
for (list_iter2 = list2.begin(); list_iter2 != list2.end(); ++list_iter2)
{
cout << *list_iter2 << " ";
}
cout << endl;
cout << "-----------------------------------------------------" << endl;
replace_copy(list2.begin(), list2.end(), ostream_iterator<int>(cout, " "), 5, 55);
cout << endl;
cout << "-----------------------------------------------------" << endl;
replace_copy_if(list2.begin(), list2.end(), ostream_iterator<int>(cout, " "), bind2nd(less<int>(), 5), 46);
cout << endl;
cout << "-----------------------------------------------------" << endl;
system("pause");
return 0;
}
===============================================
0 1 2 3 4 5 6 7 8 9
-----------------------------------------------------
0 1 2 3 4 5 42 7 8 9
-----------------------------------------------------
11 11 11 11 11 5 42 7 8 9
-----------------------------------------------------
0 1 2 3 4 5 6 4 5 6 7 8 9
-----------------------------------------------------
0 1 2 3 4 55 6 4 55 6 7 8 9
-----------------------------------------------------
46 46 46 46 46 5 6 46 5 6 7 8 9
-----------------------------------------------------
请按任意键继续. . .
C++ replace replace_if replace_copy replace_copy_if的更多相关文章
- 替换元素(replace,replace_if,replace_copy,replace_copy_if)
replace 审阅range中的每个元素,把old_value替换为new_value template <class ForwardIterator,class T> void rep ...
- 算法 replace,replace_copy,back_inserter
replace (list.begin(), list.end(), , ); // replace any elements with value of 0 by 42 replace算法对输入序列 ...
- STL--STL和她的小伙伴们:
STL--概述: 标准模板库(StandardTemplateLibrary,STL),是C++程序设计语言标准模板库.STL是由Alexander Stepanov.Meng Lee和David R ...
- STL函数模板(即算法)一览
查找算法 adjacent_find:找出一个串中第一个不符合次序的地方 find,find_if:找出第一个符合条件的元素 find_first_of:在一个串中寻找第一个与另一个串中任意一个元素相 ...
- 变易算法 - STL算法
欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/mutating-algorithms.h ...
- STL常用遍历算法for_each和transform的比较
for_each()和transform()算法比较 1)STL 算法 – 修改性算法 for_each() copy() copy_backward() transform() merge ...
- C++ STL 常用遍历算法
C++ STL 常用遍历算法 STL的容器算法迭代器的设计理念 1) STL的容器通过类模板技术,实现数据类型和容器模型的分离 2) STL的迭代器技术实现了遍历容器的统一方法:也为STL的算法提供了 ...
- STL经常使用遍历算法for_each和transform的比較
for_each()和transform()算法比較 1)STL 算法 – 改动性算法 for_each() copy() copy_backward() transform() merge ...
- STL源代码剖析——STL算法stl_algo.h
前言 在前面的博文中剖析了STL的数值算法.基本算法和set集合算法.本文剖析STL其它的算法,比如排序算法.合并算法.查找算法等等.在剖析的时候.会针对函数给出一些样例说明函数的使用.源代码出自SG ...
随机推荐
- Java学习第一天之简单了解java语言及开发环境的安装
一.初步了解Java语言 Java语言是由Sun公司的James Gosling创造的一门面向对象的高级语言. 2009年4月20日,Sun公司被Oracle以总价值约为74亿美元的价格收购,Java ...
- Server SAN
http://blog.sina.com.cn/s/blog_5946bd590102veni.html http://blog.sina.com.cn/s/blog_5946bd590102vemm ...
- Go语言中的defer
可以用作一些资源的释放. 1.在一个函数内的defer执行顺序是先写的后执行,后写的先执行(遵循栈结构) func DeferTest1(){ defer fmt.Println("我是 d ...
- Struts2 Action类的创建以及参数传递以及接收
一.Struts中Action得创建方式 1,直接创建一个简单的Action类 添加Struts.xml,配置转发方法返回转发的页面. 2,实现一个Action类 Strust.xml配置对应的Url ...
- mongodb文件
https://github.com/mongodb/mongo/tree/master 或 https://www.mongodb.com/download-center?jmp=nav#comm ...
- 基于栈的指令集与基于寄存器的指令集详细比对及JVM执行栈指令集实例剖析
基于栈的指令集与基于寄存器的指令集详细比对: 这次来学习一些新的概念:关于Java字节码的解释执行的一种方式,当然啦是一些纯理论的东东,但很重要,在之后会有详细的实验来对理论进行巩固滴,下面来了解一下 ...
- let和const的区别
es6语法中新增了 let和const 不再只是有var 1. let的用法 let是用来声明变量的,它和var的用法差不多,但是let所声明的变量只在它的代码块内有效,像for循环里用let会更好点 ...
- Python和Shell交互工具 ShellPy
ShellPy 是一款Python和Shell的交互工具.一般来说,我们会通过Subprocess.Popen或者Command模块执行一条Shell命令或脚本,然后通过返回的标准输出和错误输出来得到 ...
- 「NOI2015」荷马史诗 (k叉huffman树/k叉合并果子)
是个多叉huffman树,思想类比合并果子. 具体见 CrazyDave 的博客 CODE #include <bits/stdc++.h> using namespace std; ty ...
- 【基本算法入门-字符串哈希(Hash)】-C++
字符串哈希入门 说得通俗一点,字符串哈希实质上就是把每个不同的字符串转成不同的整数. 为什么会有这样的需要呢?很明显,存储一个超长的字符串和存储一个超大但是能存的下的整数,后者所占的空间会少的多,但主 ...