#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的更多相关文章

  1. 替换元素(replace,replace_if,replace_copy,replace_copy_if)

    replace 审阅range中的每个元素,把old_value替换为new_value template <class ForwardIterator,class T> void rep ...

  2. 算法 replace,replace_copy,back_inserter

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

  3. STL--STL和她的小伙伴们:

    STL--概述: 标准模板库(StandardTemplateLibrary,STL),是C++程序设计语言标准模板库.STL是由Alexander Stepanov.Meng Lee和David R ...

  4. STL函数模板(即算法)一览

    查找算法 adjacent_find:找出一个串中第一个不符合次序的地方 find,find_if:找出第一个符合条件的元素 find_first_of:在一个串中寻找第一个与另一个串中任意一个元素相 ...

  5. 变易算法 - STL算法

    欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/mutating-algorithms.h ...

  6. STL常用遍历算法for_each和transform的比较

    for_each()和transform()算法比较 1)STL 算法 – 修改性算法  for_each()  copy()  copy_backward()  transform()  merge ...

  7. C++ STL 常用遍历算法

    C++ STL 常用遍历算法 STL的容器算法迭代器的设计理念 1) STL的容器通过类模板技术,实现数据类型和容器模型的分离 2) STL的迭代器技术实现了遍历容器的统一方法:也为STL的算法提供了 ...

  8. STL经常使用遍历算法for_each和transform的比較

    for_each()和transform()算法比較 1)STL 算法 – 改动性算法  for_each()  copy()  copy_backward()  transform()  merge ...

  9. STL源代码剖析——STL算法stl_algo.h

    前言 在前面的博文中剖析了STL的数值算法.基本算法和set集合算法.本文剖析STL其它的算法,比如排序算法.合并算法.查找算法等等.在剖析的时候.会针对函数给出一些样例说明函数的使用.源代码出自SG ...

随机推荐

  1. smart_ptr之shared_ptr

    智能指针的概念 c++11标准和boost都提供了智能指针的功能.智能指针是普通指针的封装,智能指针是一个对象,对象里面包含了原生指针.可以使用智能指针对象的get()方法可获得封装在里面的原生指针. ...

  2. C# 开发的windows服务 不能调试——讨论整理

    CSDN的标题:C# 开发的windows服务 不能调试 System.Diagnostics.Debugger.Launch();在想加断点的地方加入这行,是进入断点的,可以进行调试,我的是xp系统 ...

  3. 【SCOI2007】降雨量

    新人求助,降雨量那题本机AC提交WAWAWA…… 原题: 我们常常会说这样的话:“X年是自Y年以来降雨量最多的”.它的含义是X年的降雨量不超过Y年,且对于任意Y<Z<X,Z年的降雨量严格小 ...

  4. C++ 中对vector<T*> 数组的查找和排序

    /* 2015-06-06 本文主要叙述对于vector<T*> (指针数组)如何进行find 操作下面的代码实现了Find,FindRange 模板函数, 解释了为什么std::find ...

  5. 查看postgresql的日志show queries log in PostgreSQL?

    原文:https://tableplus.io/blog/2018/10/how-to-show-queries-log-in-postgresql.html -------------------- ...

  6. 第93题:复原IP地址

    一. 问题描述 给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式. 示例: 输入: "25525511135" 输出: ["255.255.11.135 ...

  7. BZOJ 1565 / P2805 [NOI2009]植物大战僵尸 (最大权闭合子图 最小割)

    题意 自己看吧 BZOJ传送门 分析 - 这道题其实就是一些点,存在一些二元限制条件,即如果要选uuu则必须选vvv.求得到的权值最大是多少. 建一个图,如果选uuu必须选vvv,则uuu向vvv连边 ...

  8. BZOJ 2281: [Sdoi2011]黑白棋 (Nim游戏+dp计数)

    题意 这题目有一点问题,应该是在n个格子里有k个棋子,k是偶数.从左到右一白一黑间隔出现.有两个人不妨叫做小白和小黑.两个人轮流操作,每个人可以选 1~d 枚自己颜色的棋子,如果是白色则只能向右移动, ...

  9. Codeforces Round #584 B. Koala and Lights

    链接: https://codeforces.com/contest/1209/problem/B 题意: It is a holiday season, and Koala is decoratin ...

  10. Sort Letters by Case

    Given a string which contains only letters. Sort it by lower case first and upper case second. Examp ...