0

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);
}
4

2 に答える 2

6

あなたがやろうとしていることの標準的なイディオムは

#include <algorithm>
#include <string> 

std::string data = "Abc"; 
std::transform(data.begin(), data.end(), data.begin(), ::tolower);
于 2012-02-17T03:20:09.393 に答える
2

for_eachの戻り値は、渡した関数です。この場合は、lowです。したがって、この:

clean = for_each(temp.begin(), temp.end(), low);

これと同等です:

for_each(temp.begin(), temp.end(), low);
clean = low;

あなたが本当に欲しいのはおそらくこれです:

for_each(temp.begin(), temp.end(), low); // note: modifies temp
clean = temp;

(または、最初から削除tempして、全体で使用することもできますclean)。

于 2012-02-17T03:18:18.503 に答える