problems using STL std::transform from cygwin g++

会有一股神秘感。 提交于 2019-12-24 19:27:09

问题


I am running g++(gcc version 3.4.4) on cygwin.

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. Does anyone have any suggestions or insights into this situation. Thanks.


回答1:


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



回答2:


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. 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().



来源:https://stackoverflow.com/questions/1350380/problems-using-stl-stdtransform-from-cygwin-g

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!