简体   繁体   English

来自cygwin g ++的STL std :: transform问题

[英]problems using STL std::transform from cygwin g++

I am running g++(gcc version 3.4.4) on cygwin. 我在cygwin上运行g ++(gcc版本3.4.4)。

I can't get this small snippet of code to compile. 我无法获得这小段代码进行编译。 I included the appropriate headers. 我包括了适当的标题。

int main(){

    std::string temp("asgfsgfafgwwffw");

    std::transform(temp.begin(),
                   temp.end(),
                   temp.begin(),
                   std::toupper);

    std::cout << "result:" << temp << std::endl;

    return 0;
}

I have not had any issues using STL containers such as vector. 使用vector等STL容器时,我没有任何问题。 Does anyone have any suggestions or insights into this situation. 是否有人对此情况有任何建议或见解? Thanks. 谢谢。

From the 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. las,上面的程序将无法编译,因为名称“ toupper”不明确。 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(). 这将指示编译器选择正确的toupper()。

This explains it quite well. 这很好地解释了这一点。

Which will boil down to this code: 可以归结为以下代码:

std::transform(temp.begin(),temp.end(),temp.begin(),static_cast<int (*)(int)>(std::toupper));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM