我在实现使用 of 时遇到了麻烦for_each
,我可以用 for 循环完成工作,但为了理解起见,我可以使用一些帮助来解释......到目前为止我有以下功能......
void clean_entry(const string& orig, string& cleaned) {
size_t start = 0;
size_t end = 0;
while(!isalnum(orig[start]) && start < orig.length())
start++;
end = start;
while(isalnum(orig[end]) && end < orig.length())
end++;
cleaned = orig.substr(start,(end-start));
// I want to replace the following lines with a for_each loop
string::iterator iter;
for(iter = cleaned.begin(); iter != cleaned.end(); iter++)
*iter = tolower(*iter);
}
在我尝试为上述函数中的最后三行代码实现 for_each 循环时,我尝试了以下操作,这给了我一个编译错误......
struct {
void operator()(string::iterator strIter) {
*strIter = tolower(*strIter);
}
} lower;
for_each(cleaned.begin(),cleaned.end(),lower);