小弟我想把形如:"1 2 3"的字符串的空格去掉,这句代码为什么错
我想把形如:"1 2 3"的字符串的空格去掉,这句代码为什么错?
for(; developState.erase(developState.find_first_of( ' ')) != string::npos ; );
------解决方案--------------------
当然应该用effective stl里介绍的remove-erase联用。
------解决方案--------------------
erase(developState.find_first_of( ' ')); 擦除了啥?
erase用错了
------解决方案--------------------
写个例子给你:
#include <string>
#include <iostream>
int main( )
{
using namespace std;
string str1 ( "Hello world " );
cout < < "The original string is: " < < str1 < <endl;
str1.erase(str1.find_first_of( 'o '),1);//去掉‘o’
cout < < "The modified string is: " < < str1 < <endl;
return 0;
}
------解决方案--------------------
int main()
{
string developState = "1 2 3 4 5 ";
for(size_t t = 0; (t = developState.find_first_of( ' ')) != string::npos; developState.erase(t, 1));
cout < <developState < <endl;
return 1;
}
------解决方案--------------------
哈哈哈,搂主不要着急...
找本书先好好看看吧,呵呵
s.find_first_of( ' ')是查找第一个空格并返回空格出现的位置
s.erase(pos, len);将字符串s下标为pos处,长度为len的字符串移除,并返回操作后的s的引用
所以 Chiyer(星羽) 这样写
for(size_t t = 0; (t = developState.find_first_of( ' ')) != string::npos;
developState.erase(t, 1));
先找出字符串中空格出现的位置赋给t: t = developState.find_first_of( ' ')
接着判断t是否等于string::npos,如果相等则表示没有找到空格,循环退出。
如果t!=string::npos,则调用developState.erase(t,1);将位置t处的空格(1个字符长度)删去
一直重复直到t==string::npos为止,表示再也找不到空格了。
------解决方案--------------------
支持 taodm((不能收****社区短信息,请莫浪费精力))
#include <algorithm>
#include <string>
string str( "1 2 3 ");
str.erase(remove(str.begin(),str.end(), ' '),str.end());
for(; developState.erase(developState.find_first_of( ' ')) != string::npos ; );
------解决方案--------------------
当然应该用effective stl里介绍的remove-erase联用。
------解决方案--------------------
erase(developState.find_first_of( ' ')); 擦除了啥?
erase用错了
------解决方案--------------------
写个例子给你:
#include <string>
#include <iostream>
int main( )
{
using namespace std;
string str1 ( "Hello world " );
cout < < "The original string is: " < < str1 < <endl;
str1.erase(str1.find_first_of( 'o '),1);//去掉‘o’
cout < < "The modified string is: " < < str1 < <endl;
return 0;
}
------解决方案--------------------
int main()
{
string developState = "1 2 3 4 5 ";
for(size_t t = 0; (t = developState.find_first_of( ' ')) != string::npos; developState.erase(t, 1));
cout < <developState < <endl;
return 1;
}
------解决方案--------------------
哈哈哈,搂主不要着急...
找本书先好好看看吧,呵呵
s.find_first_of( ' ')是查找第一个空格并返回空格出现的位置
s.erase(pos, len);将字符串s下标为pos处,长度为len的字符串移除,并返回操作后的s的引用
所以 Chiyer(星羽) 这样写
for(size_t t = 0; (t = developState.find_first_of( ' ')) != string::npos;
developState.erase(t, 1));
先找出字符串中空格出现的位置赋给t: t = developState.find_first_of( ' ')
接着判断t是否等于string::npos,如果相等则表示没有找到空格,循环退出。
如果t!=string::npos,则调用developState.erase(t,1);将位置t处的空格(1个字符长度)删去
一直重复直到t==string::npos为止,表示再也找不到空格了。
------解决方案--------------------
支持 taodm((不能收****社区短信息,请莫浪费精力))
#include <algorithm>
#include <string>
string str( "1 2 3 ");
str.erase(remove(str.begin(),str.end(), ' '),str.end());