、要求删除字符串中全部给的那个字符(例如给定字符为a,字符串为aaabbd,结果应该是bbd)但是我这样写只能删除一个给定字符(结果为aabbd),这是为什么?应该怎么改进?
#include <iostream>
#include <string>
using namespace std;
void sort(string s[], int a);
int main()
{
int a = 0;
string c;
char s;
int *str = new string[30];
cin >> s;
while (cin >> c && c != "@")
{
str[a] = c;
a++;
}
for (int i = 0; i < a; i++)
{
str[i].erase(str[i].find(s),1);
}
sort(str, a);
for (int i = 0; i < a; i++)
{
cout << str[i] << endl;
}
delete str[];
return 0;
}
void sort(string s[], int a)
{
string temp;
for (int i = 0; i < a - 1; i++)
{
for (int j = i + 1; j < a; j++)
{
if (s[i] < s[j])
{
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
}
int a = 0;
char c;
char s;
cin >> s;
char *str = new char[30]; //string 改成 char
while (cin >> c && c != '@')
{
str[a] = c;
a++;
}
string str1 = str; //字符数组转成字符串
str1.resize(a); //更新字符串的大小
for (int i = 0; i < a; i++)
{
if(str1.find(s) != -1) //找到待删除的字符
str1.erase(str1.find(s), 1);
else //找不到就退出循环
break;
}