#include <iostream> #include <string> #include <map> using namespace std; /* *找出src中的target,用key替换之 * */ string &find_replace(string &src,const string &target,const string &key) { int i=0,pos; while(i<src.length()) { pos = src.find(target,i); if (pos == -1) break; src.replace(pos,target.length(),key); i += key.length(); } return src; } //用集合替换对应的 string &findSet_replace(string &src,map<string,string> s2s) { //得到搜索的集合 map<string, string>::iterator it; int i=0,pos; for(it = s2s.begin();it != s2s.end();it++) { src = find_replace(src,it->first,it->second); } return src; } int main() { string str = "--lllbbbb,my name is lb,lblb is me!"; cout<<"Target is:"<<str<<endl; find_replace(str,"lb","yanglingjuan"); cout<<str<<endl; map<string,string> s2s; s2s["-"]="X"; s2s["lin"]="BIN"; s2s[" "]="="; //map delete //s2s.erase("lin") findSet_replace(str,s2s); cout<<str<<endl; return 0; }