Replace multiple pair of characters in string

后端 未结 2 1116
情书的邮戳
情书的邮戳 2021-01-14 08:37

I want to replace all occurrence of \'a\' with \'b\', and \'c\' with \'d\'.

My current solution is:

std::replace(str.begin(), str.end(), \'a\', \'b\'         


        
2条回答
  •  死守一世寂寞
    2021-01-14 08:48

    If you do not like two passes, you can do it once:

     std::transform(std::begin(s), std::end(s), std::begin(s), [](auto ch) {
        switch (ch) {
        case 'a':
          return 'b';
        case 'c':
          return 'd';
        }
        return ch;
      });
    

提交回复
热议问题