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

转载于:https://www.cnblogs.com/herd/p/11010536.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值