c++ - 从 cygwin g++ 使用 STL std::transform 的问题

标签 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;
}

我在使用 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/

相关文章:

c++ - 如何正确创建函数头而不出现多重定义错误?

c++ - 在第二个 vector 中查找 vector 元素

c++ - 使用 std::ifstream、std::istream_iterator 和 std::copy 不读取整个文件

C++ scanf %la 返回 0

python - Cygwin 安装程序不安装 gdb.exe

c++ - C++中的日期算法

c++ - 有效处理 OOB,二维数组访问

c++ - 从 C++ 中的另一个模板方法调用模板方法?

c++ - 指针的有序关联容器

c++ - 在C++中使用STL在数组中查找最小元素