C++中删除string对象中的某个字符的问题

淡定从容我自繁华 2012-07-12 10:37:41
小白我又向各位请教啦,唉!惭愧的紧。

需求是这样的:
现在有一个string对象,如果在这个string中找到单引号,删除这个单引号。百度了好多,没找到如何删除这个单个字符的方法,请各位帮忙看看
...全文
22720 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
LAST_MAN 2012-07-12
  • 打赏
  • 举报
回复
就是楼上说的迭代器啊~楼主去看看c++ primer
IVERS0N 2012-07-12
  • 打赏
  • 举报
回复
刚学的



string str("ab'defg");
string::iterator it;
for (it =str.begin(); it != str.end(); ++it)
{
if ( *it == '\'')
{
str.erase(it);
}
}
niujian2358 2012-07-12
  • 打赏
  • 举报
回复
我认为找到单引号的索引后,获取前后俩子串,再合并子串
olderma 2012-07-12
  • 打赏
  • 举报
回复
利用string类型中find,erase。用find查找“‘ ”位置,erase删除此位置’。位置标记变量最好用string::size_type类型
tcziflw2010 2012-07-12
  • 打赏
  • 举报
回复
写错了是:
for (it =str.begin(); it != str.end();)
if(*it=='\'')
it=str.erase(it);
else ++it;
tcziflw2010 2012-07-12
  • 打赏
  • 举报
回复
当str(ab''defg)时后一个'没删
for (it =str.begin(); it != str.end();)
if(*it=='\'')
str.erase(it);
else ++it;
[Quote=引用 2 楼 的回复:]

刚学的

C/C++ code


string str("ab'defg");
string::iterator it;
for (it =str.begin(); it != str.end(); ++it)
{
if ( *it == '\'')
{
str.erase(it);
……
[/Quote]
tcziflw2010 2012-07-12
  • 打赏
  • 举报
回复
erase返回一个迭代器
[Quote=引用 7 楼 的回复:]

这样不行的,一旦删除一个节点,那么原先的迭代器就失效了。

引用 2 楼 的回复:

刚学的

C/C++ code


string str("ab'defg");
string::iterator it;
for (it =str.begin(); it != str.end(); ++it)
{
if ( *it == '\'')
{
str.erase(i……
[/Quote]
赵4老师 2012-07-12
  • 打赏
  • 举报
回复
basic_string::erase
iterator erase(iterator first, iterator last);
iterator erase(iterator it);
basic_string& erase(size_type p0 = 0, size_type n = npos);
The first member function removes the elements of the controlled sequence in the range [first, last). The second member function removes the element of the controlled sequence pointed to by it. Both return an iterator that designates the first element remaining beyond any elements removed, or end() if no such element exists.

The third member function removes up to n elements of the controlled sequence beginning at position p0, then returns *this.
雨中的月儿 2012-07-12
  • 打赏
  • 举报
回复
左右分隔再合并的方法,会浪费内存copy,尤其是在有很多个单引号的情况下。
比如,string s1 = "'A'B'C'D'E'F'G";
时间效率和空间效率都比较低,而且很容易出错。

如果使用从后向前找,然后memmov的方法,会比左右分隔合并好一些。但还不够好。

个人感觉,一次遍历,然后另外用一个数组记住每个单引号的位置,再针对性的memcpy或memmov,应该是最好的方法了。

cobra_chen 2012-07-12
  • 打赏
  • 举报
回复
这样不行的,一旦删除一个节点,那么原先的迭代器就失效了。

[Quote=引用 2 楼 的回复:]

刚学的

C/C++ code


string str("ab'defg");
string::iterator it;
for (it =str.begin(); it != str.end(); ++it)
{
if ( *it == '\'')
{
str.erase(it);
……
[/Quote]
yuanguang 2012-07-12
  • 打赏
  • 举报
回复
更简单高效的方法是
string s = "123'456";
s.erase ( remove ( vec.begin() , vec.end() , '\'' ),vec.end() ) ;
string也是一种容器,可以用算法的。
酱油党 2012-07-12
  • 打赏
  • 举报
回复
	string abc = "123'456";
while( 1 )
{
int nPos = abc.find_first_of( '\'' );
if ( nPos != string::npos )
{
abc = abc.substr( 0 , nPos ) + abc.substr( nPos + 1 , -1 );
}
else
break;
}
这个东西办法很多的,并不一定要使用迭代器,但是如果会用的话还是使用迭代器效率好一些
  • 打赏
  • 举报
回复
非常感谢2楼,感谢大家的回复

64,676

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧