字符串问题 [英] Problem with string
问题描述
我做了这个功能:
void clean(字符串& s)
{
for(size_t i = 0 ; i< s.size(); ++ i)
{
if(isalpha(s [i]))
s [i ] = tolower(s [i]);
else
s.erase(i,1);
}
返回;
}
我有一个像这样的字符串的奇怪行为:
迪士尼乐园 - >迪士尼乐园
" .disneyland。" - > .disneyland"
似乎擦除功能正在发生的事情
i不知道,因为有这个:
void clean(string& s)
{
string s2;
for(size_t i = 0 ; i< s.size(); ++ i)
{
if(isalpha(s [i]))
{
s [i] = tolower(s [i]);
s2 + = s [i];
}
}
s = s2;
返回;
}
一切顺利。
第一个代码出了什么问题?
谢谢。
Gaijinco写道:我做了这个功能:
void clean(string& s)
(size_t i = 0; i< s.size(); ++ i)
{
if(isalpha(s [i]))
s [i] = tolower(s [ i]);
s.erase(i,1);
}
返回;
}
调用erase()不只是擦掉内容并在字符串中留下了一个空洞。它将后续内容向前移动以替换要删除的部分
。
由于移动内容,您的索引我不再索引到
你想要的角色。
我有一个像这样的字符串的奇怪行为:
迪斯尼乐园 - >迪斯尼乐园
.disneyland。 - > .disneyland"
似乎擦除功能正在发生什么事情,我不知道,因为有这个:
....
void clean(string& s)
{
string s2;
for(size_t i = 0; i< s.size( ); ++ i)
{
if(isalpha(s [i]))
{
s [i] = tolower(s [i]);
s2 + = s [i];
}
}
s = s2;
返回;
}
一切顺利。
是的,这是正确的方法。这是一个更清洁,更快速,并且正确的实施。
第一个代码有什么问题?
指数很像迭代器,它们是无效的。当你从容器中擦除
时。
谢谢。
Ben
Gaijinco写道:我做了这个功能:
void clean(string& s)
for( size_t i = 0; i< s.size(); ++ i)
{
if(isalpha(s [i]))
s [i] = tolower(s [i ]);
s.erase(i,1);
}
返回;
}
我有像这样的字符串的奇怪行为:
迪士尼乐园 - >迪斯尼乐园
.disneyland。 - > .disneyland"
似乎擦除功能正在发生什么事情我不知道,因为有了这个:
无效的(字符串& s)
{
字符串s2;
for(size_t i = 0; i< s.size(); ++ i)
{
if (isalpha(s [i]))
{
s [i] = tolower(s [i]);
s2 + = s [i];
}
}
s = s2;
返回;
}
一切顺利。
第一个代码出了什么问题?
谢谢。
是的,擦除()删除了字符,因此大小已经改变。
这是一个快速而又肮脏的例子:
#include< iostream>
#include< string>
# include< cctype>
void clean(std :: string& s)
{
std :: string ::迭代它;
//使用迭代器它逐步通过字符串
for(it = s.begin(); it!= s.end();)
{
//如果当前字符是alpha
if(isalpha(* it) ))
{
//将字符改为小写
* it = tolower(* it);
//递增''它'到下一个字母
++ it;
}
else
{
//从's'中删除当前的非alpha字符。
//重置''它''指向
之后的第一个字符//刚被删除的字符 - 或者是s.end()如果没有
//'s'中有更多字符。
it = s.erase(it);
}
}
返回;
}
int main()
{
std :: string t1 =" Disneyland";
std :: string t2 =" .disneyland。" ;;
clean(t1);
clean(t2) ;
std :: cout<< t1<< std :: endl;
std :: cout<< t2<< std :: endl;
返回0;
}
此程序输出:
迪士尼乐园
迪士尼乐园
问候,
拉里
Larry I Smith写道:Gaijinco写道:我做了这个功能:
void clean(string& s)
{
for(size_t i = 0; i< s.size(); ++ i)
{
if(isalpha(s [i]))
s [i] = tolower(s [i]);
s.erase(i,1);
}
返回;
}
我有一个像这样的字符串的奇怪行为:
迪士尼乐园 - >迪斯尼乐园
.disneyland。 - > .disneyland"
似乎擦除功能正在发生什么事情我不知道,因为有了这个:
无效的(字符串& s)
{
字符串s2;
for(size_t i = 0; i< s.size(); ++ i)
{
if (isalpha(s [i]))
{
s [i] = tolower(s [i]);
s2 + = s [i];
}
}
s = s2;
返回;
}
一切顺利。
第一个代码出了什么问题?
谢谢。
是的,擦除()删除了字符,因此大小已经改变。
这是一个快速而肮脏的例子:
#include< iostream>
#include< string>
#include< cctype>
void clean(std :: string& ; s)
{
std :: string :: iterator it;
//使用迭代器''它'来逐步通过字符串(它) = s.begin();它!= s.end();)
{
// i f当前的char是一个alpha
if(isalpha(* it))
//将char更改为小写
* it = tolower(* it);
//将''它''递增到下一个字符串
++ it;
}
{
//删除来自's'的当前非alpha字符。
//重置''它''指向
//刚刚删除的第一个字符 - 或者指向s.end()if没有
//'s'中的更多字符。
它= s.erase(it);
}
}
返回;
}
std :: string t1 =" Disneyland";
std :: string t2 =" .disneyland 。;
干净(t1);
干净(t2);
std :: cout<< t1<< std :: endl;
std :: cout<< t2<< std :: endl;
返回0;
}
此程序输出:
disneyland
disneyland >
问候,
Larry
在这种情况下,我更喜欢以相反的顺序遍历容器。
在这种情况下,我们已经检查过的容器部分已经转移并且没有问题。
你不同意吗?
I did this function:
void clean(string&s)
{
for(size_t i=0; i<s.size(); ++i)
{
if(isalpha(s[i]))
s[i]=tolower(s[i]);
else
s.erase(i,1);
}
return;
}
I''m having a strange behavior with string like these:
"Disneyland" -> Disneyland
".disneyland." -> .disneyland"
It seems as if there is something going on with the erase function that
i don''t know, because with this:
void clean(string&s)
{
string s2;
for(size_t i=0; i<s.size(); ++i)
{
if(isalpha(s[i]))
{
s[i]=tolower(s[i]);
s2+=s[i];
}
}
s=s2;
return;
}
Everything goes OK.
What''s wrong with the first code?
Thanks.
Gaijinco wrote:I did this function:
void clean(string&s)
{
for(size_t i=0; i<s.size(); ++i)
{
if(isalpha(s[i]))
s[i]=tolower(s[i]);
else
s.erase(i,1);
}
return;
}
A call to erase() does not just rub off the content and left a hole in
the string. It moves the subsequent content forward to replace the part
to be removed.
As a result of moving the content, your index i no longer indexes into
the character that you want.
I''m having a strange behavior with string like these:
"Disneyland" -> Disneyland
".disneyland." -> .disneyland"
It seems as if there is something going on with the erase function that
i don''t know, because with this:
....
void clean(string&s)
{
string s2;
for(size_t i=0; i<s.size(); ++i)
{
if(isalpha(s[i]))
{
s[i]=tolower(s[i]);
s2+=s[i];
}
}
s=s2;
return;
}
Everything goes OK.
Yep, this is the right way to do it. It is a cleaner, faster and, above
all, correct implementation.
What''s wrong with the first code?
Index are much like iterators, they are "invalidated" when you erase
something from a container.
Thanks.
Ben
Gaijinco wrote:I did this function:
void clean(string&s)
{
for(size_t i=0; i<s.size(); ++i)
{
if(isalpha(s[i]))
s[i]=tolower(s[i]);
else
s.erase(i,1);
}
return;
}
I''m having a strange behavior with string like these:
"Disneyland" -> Disneyland
".disneyland." -> .disneyland"
It seems as if there is something going on with the erase function that
i don''t know, because with this:
void clean(string&s)
{
string s2;
for(size_t i=0; i<s.size(); ++i)
{
if(isalpha(s[i]))
{
s[i]=tolower(s[i]);
s2+=s[i];
}
}
s=s2;
return;
}
Everything goes OK.
What''s wrong with the first code?
Thanks.
Well yes, erase() removes chars, so the size has changed.
Here''s a quick and dirty example:
#include <iostream>
#include <string>
#include <cctype>
void clean( std::string& s )
{
std::string::iterator it;
// step thru the string using the iterator ''it''
for( it = s.begin(); it != s.end(); )
{
// if the current char is an alpha
if( isalpha(*it) )
{
// change the char to lower case
*it = tolower(*it);
// increment ''it'' to the next char
++it;
}
else
{
// delete the current non-alpha char from ''s''.
// resets ''it'' to point to the first char after
// the one just deleted - or to s.end() if no
// more chars in ''s''.
it = s.erase(it);
}
}
return;
}
int main()
{
std::string t1 = "Disneyland";
std::string t2 = ".disneyland.";
clean(t1);
clean(t2);
std::cout << t1 << std::endl;
std::cout << t2 << std::endl;
return 0;
}
This program outputs:
disneyland
disneyland
Regards,
Larry
Larry I Smith wrote:Gaijinco wrote:I did this function:
void clean(string&s)
{
for(size_t i=0; i<s.size(); ++i)
{
if(isalpha(s[i]))
s[i]=tolower(s[i]);
else
s.erase(i,1);
}
return;
}
I''m having a strange behavior with string like these:
"Disneyland" -> Disneyland
".disneyland." -> .disneyland"
It seems as if there is something going on with the erase function that
i don''t know, because with this:
void clean(string&s)
{
string s2;
for(size_t i=0; i<s.size(); ++i)
{
if(isalpha(s[i]))
{
s[i]=tolower(s[i]);
s2+=s[i];
}
}
s=s2;
return;
}
Everything goes OK.
What''s wrong with the first code?
Thanks.
Well yes, erase() removes chars, so the size has changed.
Here''s a quick and dirty example:
#include <iostream>
#include <string>
#include <cctype>
void clean( std::string& s )
{
std::string::iterator it;
// step thru the string using the iterator ''it''
for( it = s.begin(); it != s.end(); )
{
// if the current char is an alpha
if( isalpha(*it) )
{
// change the char to lower case
*it = tolower(*it);
// increment ''it'' to the next char
++it;
}
else
{
// delete the current non-alpha char from ''s''.
// resets ''it'' to point to the first char after
// the one just deleted - or to s.end() if no
// more chars in ''s''.
it = s.erase(it);
}
}
return;
}
int main()
{
std::string t1 = "Disneyland";
std::string t2 = ".disneyland.";
clean(t1);
clean(t2);
std::cout << t1 << std::endl;
std::cout << t2 << std::endl;
return 0;
}
This program outputs:
disneyland
disneyland
Regards,
Larry
In cases like this, i prefer to traverse the container in reverse order.
In that case the part of the container we have already examined is
shifted and presents no problem.
Wouldn''t you agree?
这篇关于字符串问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!