【发布时间】:2015-05-15 03:20:04
【问题描述】:
我正在尝试使用 std::toupper 函数将字符串的小写字符转换为对应的大写字符,并且我正在使用 std::for_each 算法迭代字符串中的字符。
#include <iostream>
#include <string>
#include <algorithm>
#include <locale>
std::string convert_toupper(std::string *x) {
return std::toupper(*x, std::locale());
}
int main() {
std::string x ("example");
std::for_each(x.begin(), x.end(), convert_toupper);
}
当我编译这段代码时,我收到了这个错误:
In file included from /usr/include/c++/4.8/algorithm:62:0,
from me2.cpp:3:
/usr/include/c++/4.8/bits/stl_algo.h: In instantiation of ‘_Funct std::for_each(_IIter, _IIter, _Funct) [with _IIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >; _Funct = std::basic_string<char> (*)(std::basic_string<char>*)]’:
me2.cpp:13:52: required from here
/usr/include/c++/4.8/bits/stl_algo.h:4417:14: error: invalid conversion from ‘char’ to ‘std::basic_string<char>*’ [-fpermissive]
__f(*__first);
^
使用 std::toupper 和 std::for_each 将字符从小写转换为大写的正确方法是什么?
【问题讨论】: