C++ STL(11):元素替换

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <iterator>
#include <functional>

//元素替换
int main()
{
     /************************************************************************/
     //replace
     /************************************************************************/
     //将range[first,last)内的oldval替换乘newval
     /*
     template<class ForwardIterator, class Type>
     void replace(
          ForwardIterator _First,
          ForwardIterator _Last,
          const Type& _OldVal,
          const Type& _NewVal
          );
     */
     int a[] = { 1, 3, 6, 8, 1, 2, 4, 0, 1 };
     
     //1 3 6 8 1 2 4 0 1
     std::for_each(std::begin(a), std::end(a), [](const int i){std::cout << i << " "; });
     std::cout << std::endl;
     std::replace(std::begin(a), std::end(a), 1, -1);
     //-1 3 6 8 -1 2 4 0 -1
     std::for_each(std::begin(a), std::end(a),[](const int i){std::cout << i << " "; });
     std::cout << std::endl;
 

     std::vector<std::string> fruits;
     fruits.push_back("apple");
     fruits.push_back("orange");
     fruits.push_back("pear");
     fruits.push_back("banana");
     fruits.push_back("peach");
     fruits.push_back("apple");
 
     //apple orange pear banana peach apple
     std::copy(fruits.begin(), fruits.end(), std::ostream_iterator<std::string>(std::cout, " "));
     std::cout << std::endl;
      std::replace(fruits.begin(), fruits.end(), std::string("apple"), std::string("plum"));
     //plum orange pear banana peach plum
     std::copy(fruits.begin(), fruits.end(), std::ostream_iterator<std::string>(std::cout, " "));
     std::cout << std::endl;


     /************************************************************************/
     //replace_if
     /************************************************************************/
     //replace_if:将范围[first,last)内满足某种条件的val替换为新的val值
     /*
     template<class ForwardIterator, class Predicate, class Type>
     void replace_if(
          ForwardIterator _First,
          ForwardIterator _Last,
          Predicate _Pred,
          const Type& _Val
          );
     */
     //把小于0的数替换为0
     int b[] = { -1, 4, -3, -9, 2, 8, -7 };
     
     //-1 4 -3 -9 2 8 -7
     std::copy(std::begin(b), std::end(b), std::ostream_iterator<int>(std::cout, " "));
     std::cout << std::endl;
     std::replace_if(std::begin(b), std::end(b),std::bind1st(std::greater<int>(), 0), 0);
     //0 4 0 0 2 8 0
     std::copy(std::begin(b), std::end(b), std::ostream_iterator<int>(std::cout, " "));
     std::cout << std::endl;

     //将字符串长度大于n位的用"******"替换
     struct string_length_limit
     {
          string_length_limit(size_t nLimit) :limit(nLimit){}
          bool operator()(const std::string& s)
          {
               return (s.size() > limit);
          }
          size_t limit;
     };

     std::string ss[7] = {"oxygen", "carbon", "nitrogen", "iron", "sodiym","hydrogen", "silicon" };
     
     //oxygen carbon nitrogen iron sodiym hydrogen silicon
     std::copy(std::begin(ss), std::end(ss), std::ostream_iterator<std::string>(std::cout, " "));
     std::cout << std::endl;
     std::replace_if(std::begin(ss), std::end(ss), string_length_limit(6), "******");
     //oxygen carbon ****** iron sodiym ****** ******
     std::copy(std::begin(ss), std::end(ss), std::ostream_iterator<std::string>(std::cout, " "));
     std::cout << std::endl;
 

     /************************************************************************/
     //replace_copy
     /************************************************************************/
     //replace_copy:将范围[first,last)内的元素复制到范围[result,result+(last-first))内,
     //  在复制过程中将oldval替换成newval,原范围[first,last)内的元素不会更动
     /*
     template<class InputIterator, class OutputIterator, class Type>
     OutputIterator replace_copy(
          InputIterator _First,
          InputIterator _Last,
          OutputIterator _Result,
          const Type& _OldVal,
          const Type& _NewVal
          );
     */
     std::vector<int> iv1{ 1, -2, 3, 1 };
     std::vector<int> iv2(iv1.size());

     std::copy(std::begin(iv1), std::end(iv1), std::ostream_iterator<int>(std::cout, " "));
     std::cout << std::endl;    //1 -2 3 1
     std::replace_copy(iv1.begin(), iv1.end(), iv2.begin(), 1, 0);
     std::copy(std::begin(iv1), std::end(iv1), std::ostream_iterator<int>(std::cout, " "));
     std::cout << std::endl;    //1 -2 3 1
     std::copy(std::begin(iv2), std::end(iv2), std::ostream_iterator<int>(std::cout, " "));
     std::cout << std::endl;    //0 -2 3 0

 
     /************************************************************************/
     //replace_copy_if
     /************************************************************************/
     //replace_copy_if:行为类似copy,replace_if的结合,先copy,然后replace_if
     /*
     template<class InputIterator, class OutputIterator, class Predicate, class Type>
     OutputIterator replace_copy_if(
          InputIterator _First,
          InputIterator _Last,
          OutputIterator _Result,
          Predicate _Pred,
          const Type& _Val
          );
     */
      std::replace_copy_if(iv1.begin(), iv1.end(),std::ostream_iterator<int>(std::cout, " "),
                  std::bind2nd(std::less<int>(), 0), 0);    //1 0 3 1
     std::cout << std::endl;
 
     return 0;
}

====================打个广告,欢迎关注====================

QQ:412425870
csdn博客:
http://blog.youkuaiyun.com/caychen
码云:
https://gitee.com/caychen/
github:
https://github.com/caychen

点击群号或者扫描二维码即可加入QQ群:

328243383(1群)



点击群号或者扫描二维码即可加入QQ群:

180479701(2群)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值