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

发布于 2024-08-02 20:09:19 字数 427 浏览 4 评论 0原文

我正在 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 容器(例如矢量)时没有遇到任何问题。 有没有人对这种情况有任何建议或见解。 谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听风吹 2024-08-09 20:09:19

来自上面的链接

#include ; // 对于礼帽
#include <字符串>
#include <算法>
使用命名空间 std;

无效主()
{
字符串 s="你好";
变换(s.begin(),s.end(),s.begin(),toupper);
}

唉,上面的程序不会
编译,因为名称 'toupper' 是
模糊的。它可以指:

int std::toupper(int); // 来自 

模板<类图> 
  charT std::toupper(charT, const locale&);// 来自 
  <区域设置>

使用显式强制转换来解析
歧义:

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

这将指示编译器
选择正确的 toupper()。

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

缘字诀 2024-08-09 20:09:19

这很好地解释了这一点。

这将归结为以下代码:

std::transform(temp.begin(),temp.end(),temp.begin(),static_cast<int (*)(int)>(std::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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文