zoukankan      html  css  js  c++  java
  • 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
    -----------------------------------------------------
    请按任意键继续. . .

  • 相关阅读:
    存储过程
    springMVC学习
    springMVC学习
    学习之hibernate下册
    学习hibernate上册
    学习Struts2框架之下册
    学习Struts2框架上册
    四道java语言练习基础题:
    在开发第一个Android应用之前需要知道的5件事:
    JAVA程序员必须要学习的知识
  • 原文地址:https://www.cnblogs.com/herd/p/11010536.html
Copyright © 2011-2022 走看看