string字符串删除指定字符c++
时间: 2023-12-08 17:03:23 浏览: 208
可以使用STL的`erase`函数结合`remove`算法来实现删除字符串中指定字符c的操作。具体代码如下:
```c++
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
string s = "hello world";
char c = 'l';
s.erase(remove(s.begin(), s.end(), c), s.end());
cout << s << endl;
return 0;
}
```
输出结果为:
```
heo word
```
其中,`remove`算法会将所有等于指定字符c的元素移到字符串的末尾,并返回一个迭代器,该迭代器指向第一个被移动到末尾的元素位置。然后,`erase`函数根据该迭代器删除从迭代器指向位置到字符串末尾的所有元素,从而实现删除指定字符c的操作。
阅读全文
相关推荐















