我在 cygwin 上运行 g++(gcc 版本 3.4.4)。
我无法编译这一小段代码。我包含了适当的标题。
int main(){
std::string temp("asgfsgfafgwwffw");
std::transform(temp.begin(),
temp.end(),
temp.begin(),
std::toupper);
std::cout << "result:" << temp << std::endl;
return 0;
}
我在使用 Vector 等 STL 容器时没有遇到任何问题。 有没有人对这种情况有任何建议或见解。 谢谢。
最佳答案
来自link above .
#include <cctype> // for toupper #include <string> #include <algorithm> using namespace std; void main() { string s="hello"; transform(s.begin(), s.end(), s.begin(), toupper); }
Alas, the program above will not compile because the name 'toupper' is ambiguous. It can refer either to:
int std::toupper(int); // from <cctype>
or
template <class chart> charT std::toupper(charT, const locale&);// from <locale>
Use an explicit cast to resolve the ambiguity:
std::transform(s.begin(), s.end(), s.begin(), (int(*)(int)) toupper);
This will instruct the compiler to choose the right toupper().
关于c++ - 从 cygwin g++ 使用 STL std::transform 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1350380/