std::toupper
オーバーロードされた関数です。そのため<unresolved overloaded function type>
、エラー メッセージが表示されます。特定のオーバーロードを選択するには、それをキャストする必要があります。
static_cast<int(*)(int)>(std::toupper)
for_each
また、このタスクには適切な選択ではありませんtoupper
。リスト内の各文字列を呼び出し、結果を破棄します。std::transform
が適切な選択です。出力イテレータに出力を書き込みます。ただし、toupper
文字列ではなく文字で機能します。文字列内の各文字transform
を呼び出すために引き続き使用できます。toupper
std::transform(
a_string.begin(),
a_string.end(),
a_string.begin(),
static_cast<int(*)(int)>(std::toupper)
);
この単純なケースでは、ループを使用する方がおそらく明確です。
for (TVector::iterator i = a_list.begin(), end = a_list.end(); i != end; ++i) {
for (std::string::size_type j = 0; j < i->size(); ++j) {
(*i)[j] = toupper((*i)[j]);
}
}
<algorithm>
しかし、ツールだけで書きたい場合は<iterator>
、ファンクターを作成できます。
struct string_to_upper {
std::string operator()(const std::string& input) const {
std::string output;
std::transform(
input.begin(),
input.end(),
std::back_inserter(output),
static_cast<int(*)(int)>(std::toupper)
);
return output;
}
};
// ...
std::transform(
a_list.begin(),
a_list.end(),
a_list.begin(),
string_to_upper()
);