将 for_each 与 tolower() 一起使用
我正在尝试使用 STL 函数 for_each 将字符串转换为小写,但我不知道我做错了什么。这是有问题的 for_each 行:
clean = for_each(temp.begin(), temp.end(), low);
其中 temp 是保存字符串的字符串。这是我为 low 编写的函数:
void low(char& x)
{
x = tolower(x);
}
我不断收到的编译器错误是这样的:
error: invalid conversion from void (*)(char&) to char [-fpermissive]
我做错了什么?
编辑: 这是我正在编写的整个函数:
void clean_entry (const string& orig, string& clean)
{
string temp;
int beginit, endit;
beginit = find_if(orig.begin(), orig.end(), alnum) - orig.begin();
endit = find_if(orig.begin()+beginit, orig.end(), notalnum) - orig.begin();
temp = orig.substr(beginit, endit - beginit);
clean = for_each(temp.begin(), temp.end(), low);
}
I am trying to use the STL function for_each to convert a string into lower case and I have no idea what I am doing wrong. Here's the for_each line in question:
clean = for_each(temp.begin(), temp.end(), low);
Where temp is a string that is holding a string. And here's the function that I wrote for low:
void low(char& x)
{
x = tolower(x);
}
And the compiler error that I keep getting is as such:
error: invalid conversion from void (*)(char&) to char [-fpermissive]
What am I doing wrong?
EDIT:
Here is the whole function that I am writing:
void clean_entry (const string& orig, string& clean)
{
string temp;
int beginit, endit;
beginit = find_if(orig.begin(), orig.end(), alnum) - orig.begin();
endit = find_if(orig.begin()+beginit, orig.end(), notalnum) - orig.begin();
temp = orig.substr(beginit, endit - beginit);
clean = for_each(temp.begin(), temp.end(), low);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想要做的事情的标准习惯用法是
The standard idiom for what you are trying to do is
for_each
的返回值是您传递给它的函数 - 在本例中为low
。所以这个:相当于这个:
当你真正想要的可能是这样的时候:(
或者你可以从一开始就消除
temp
,并在整个过程中使用clean
)。for_each
's return-value is the function that you passed it — in this case,low
. So this:is equivalent to this:
when what you really want is probably this:
(or you can just eliminate
temp
to begin with, and useclean
throughout).