0 c++ foreach stl g++ stl-algorithm
我试图使用STL函数for_each将字符串转换为小写,我不知道我做错了什么.这是有问题的for_each行:
clean = for_each(temp.begin(), temp.end(), low);
其中temp是一个包含字符串的字符串.这是我写的低功能:
void low(char& x)
{
x = tolower(x);
}
Run Code Online (Sandbox Code Playgroud)
我一直得到的编译器错误是这样的:
error: invalid conversion from void (*)(char&) to char [-fpermissive]
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
编辑:这是我写的整个功能:
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);
}
Run Code Online (Sandbox Code Playgroud)
小智 6
你想要做的标准习语是
#include <algorithm>
#include <string>
std::string data = "Abc";
std::transform(data.begin(), data.end(), data.begin(), ::tolower);
Run Code Online (Sandbox Code Playgroud)