gpt4 book ai didi

c++ - 找不到 'std::transform..."的匹配项

转载 作者:行者123 更新时间:2023-11-30 04:16:55 25 4
gpt4 key购买 nike

我有一个奇怪的错误,代码之前可以运行,但一段时间后它停止编译。错误是:

Could not find a match for 'std::transform<InputIterator,OutputIterator,UnaryOperation>(char *,char *,char *,charT (*)(charT,const locale &))' in function main() 

它所指的行是:

    string ans;
cin>>ans;
std::transform(ans.begin(), ans.end(), ans.begin(), ::tolower);

有人可以帮我看看为什么会这样吗?我使用的包括:

#include <fstream.h>;
#include <iostream.h>;
#include <string>;
#include <time.h>;
#include <vector>;
using namespace std;

非常感谢

最佳答案

如果如您所说,这直到最近才奏效,我必须假设有人在代码的其他地方引入了一个小改动,从而破坏了一些东西。现在,这有效:

#include <string>
#include <algorithm>
#include <cctype>
#include <iterator>
#include <iostream>

int main()
{
std::string s1 {"Hello"}, s2;
std::transform(
std::begin(s1),
std::end(s1),
std::back_inserter(s2),
::tolower);
std::cout << s2 << '\n';
}

即它打印 hello。如果我在顶部添加这两行:

#include <locale>
using std::tolower;

我遇到了与您类似的错误(不完全相同)。这是因为它带来了this version of tolower入范围。要取回“正确的”版本(假设您指的是 cctype header 中的版本?)您可以使用 static_cast 来选择您想要的版本:

// ...

#include <locale>
using std::tolower;

int main()
{
std::string s1 {"Hello"}, s2;
std::transform(
std::begin(s1),
std::end(s1),
std::back_inserter(s2),
static_cast<int(*)(int)>(::tolower)); // Cast picks the correct fn.
std::cout << s2 << '\n';
}

编辑:我不得不说,我很困惑为什么你要专门选择那个版本,而不是得到一个模棱两可的错误。但我无法准确猜测您的代码中发生了什么变化...

关于c++ - 找不到 'std::transform..."的匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17441543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com