字符串替换 hihoCoder1082 然而沼跃鱼早就看穿了一切

来源:互联网 发布:中国gdp增速放缓知乎 编辑:程序博客网 时间:2024/06/07 12:50

一直因为C++里面没有现成的替换函数而烦恼,只怪自己太懒懒的写

借这题写好这个函数,,以后直接用就好了

#include<map>#include<set>#include<cmath>#include<stack>#include<queue>#include<cstdio>#include<string>#include<vector>#include<cstring>#include<iostream>#include<algorithm>#include<functional>using namespace std;const int MX = 1e3 + 5;string Replace(string s, string a, string b, bool Match = false) {    string tmp = s;    if(!Match) {        transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);        transform(a.begin(), a.end(), a.begin(), ::tolower);    }    int pos;    while(true) {        if((pos = tmp.find(a)) != -1) {            tmp.replace(pos, a.length(), b);            s.replace(pos, a.length(), b);        } else break;    }    return s;}char S[MX], a[MX] = "marshtomp", b[MX] = "fjxmlhx";int main() {    while(gets(S)) {        printf("%s\n", Replace(string(S), string(a), string(b)).c_str());    }    return 0;}


0 0
原创粉丝点击