• 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
    -----------------------------------------------------
    请按任意键继续. . .

  • 相关阅读:
    50种方法巧妙优化你的SQL Server数据库
    sql server数据库中选出指定范围的行的sql语句写法
    如何开启SQLSERVER数据库缓存依赖优化网站性能
    网站项目建设流程概述
    一个flash网页图片播放器
    ASP.NET中备份sqlserver数据库的方法
    ASP.NET(C#)实现一次性上传多张图片(多个文件)
    GridView使用技巧之:新增记录、GridView内数据验证、删除信息提示等
    FreeTextBox控件上传图片到指定的绝对路径的改进
    Linux环境下的32位与64位
  • 原文地址:https://www.cnblogs.com/herd/p/11010536.html
Copyright © 2020-2023  润新知