【问题标题】:problems using STL std::transform from cygwin g++使用来自 cygwin g++ 的 STL std::transform 的问题
【发布时间】:2009-08-29 03:43:01
【问题描述】:

我在 cygwin 上运行 g++(gcc 版本 3.4.4)。

我无法编译这么小的 sn-p 代码。我包括了适当的标题。

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 容器时没有遇到任何问题。 有没有人对这种情况有任何建议或见解。 谢谢。

【问题讨论】:

  • 你能把错误信息也贴出来吗?
  • 什么是“适当的标题”,您遇到了哪个错误?您的问题也可能只是“我正在编写正确的代码,但它不起作用。怎么了?”我们需要知道 1) 你在做什么,以及 2) 错误是什么。
  • 有一个很好的解释这个问题(和它的解决方案)here

标签: c++ stl cygwin transform


【解决方案1】:

来自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()。

【讨论】:

    【解决方案2】:

    This explains it quite well.

    这将归结为这段代码:

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

    【讨论】:

    • 请在帖子中解释问题。外部文章可能会被撤下,然后您的回答将毫无用处。另外,为了知道您的答案是否正确,我们所有人都必须点击外部链接,这很痛苦。
    • 我很抱歉错过了这个显而易见的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2011-01-29
    • 2020-07-26
    • 1970-01-01
    • 1970-01-01
    • 2019-12-24
    • 2022-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多