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

时间:2009-08-29 03:43:01

标签: c++ stl cygwin transform

我在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;
}

使用STL容器(例如vector)时没有任何问题。 有没有人对这种情况有任何建议或见解。 感谢。

2 个答案:

答案 0 :(得分:2)

来自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);
}
     唉,上面的程序不会   编译,因为名称'toupper'是   暧昧。它可以指代:

int std::toupper(int); // from <cctype>
     

template <class chart> 
  charT std::toupper(charT, const locale&);// from 
  <locale>
     

使用显式强制转换解决问题   歧义:

std::transform(s.begin(), s.end(), s.begin(), 
               (int(*)(int)) toupper);
     

这将指示编译器   选择合适的toupper()。

答案 1 :(得分:0)

This explains it quite well.

这将归结为此代码:

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