题目很长,我就直接发相应的函数了。
string remove1(string string1, char index) {
while(string1.find(index, 0) != string::npos) {
string1 = remove2(string1, string1.find(index, 0));
}
return string1;
}
string remove2(string string1, unsigned int position) {
string result;
if (position < 0 || position >= string1.length()) {
return string1;
}
result.append(string1, 0, position-1);
result.append(string1, position, string1.length() - position);
return result;
}
其中remove2函数是删除掉position处的字符,remove1配合find函数是删除掉index的字符,但是我自己测试的时候一调用就出现bad_alloc错误,赋值我已经在main函数中完成,这里只传值。
希望大神不吝赐教。
string remove1(string string1, char index) {
while(string1.find(index, 0) != string::npos) {
string1 = remove2(string1, string1.find(index, 0));
}
return string1;
}
string remove2(string string1, unsigned int position) {
string result;
if (position < 0 || position >= string1.length()) {
return string1;
}
result.append(string1, 0, position-1);
result.append(string1, position, string1.length() - position);
return result;
}
其中remove2函数是删除掉position处的字符,remove1配合find函数是删除掉index的字符,但是我自己测试的时候一调用就出现bad_alloc错误,赋值我已经在main函数中完成,这里只传值。
希望大神不吝赐教。