请教!关于erase函数的问题~!

cocly cocly 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
1)对String使用erase函数
cin>>str;
string::iterator strIt = str.begin();
while(strIt != str.end())
{
if(*strIt == 'A')
strIt = str.erase(strIt);
}

目的:想在str字符串里面删除所有A字符,为什么会在while语句里面出现死循环。

如果用下面方式实现就可以:
string::iterator temp;
for(unsigned int is = 0; is < str.size(); is++)
{
temp = find(str.begin(), str.end(), 'A');
if(temp != str.end())
{
str.erase(temp);
}
}

请教一下大家,谢谢~!
...全文

相关推荐

评论 14
收藏 已收藏
分享
返回顶部
cocly
C++ 语言 65,214 成员 250,514 内容