C/C++中怎样删除字符串中与另一个字符串中相同的字符

来源:百度知道 编辑:UC知道 时间:2024/05/21 06:59:25
编写一个从串S1中删去与串S2中的相同的所有字符的算法
是算法与数据结构中的题目 最好不用系统函数

#include <iostream>
#include <string>
#include <cstddef>
using namespace std;

int main()
{
string str1, str2;

cin >> str1 >> str2;
size_t index = 0;

while((index = str1.find(str2)) != string::npos)
{
str1.erase(index, str2.size());
}

cout << str1;
}

string s("So, you like donuts, eh? Well, have all the donuts in the world!");
cout << "The original string is '" << s << "'" << endl;

s.erase( 50, 14 );
cout << "Now the string is '" << s << "'" << endl;

s.erase( 24 );
cout << "Now the string is '" << s << "'" << endl;

s.erase();
cout << "Now the string is '" << s <<