找不到与"std::transform..." 的匹配项

Could not find a match for 'std::transform..."

本文关键字:transform std 找不到      更新时间:2023-10-16

我有一个奇怪的错误,代码以前可以工作,但过了一段时间就停止了编译。错误为:

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;

我得到了一个与你相似的错误(不完全相同)。这是因为它将此版本的tolower纳入范围。要返回"正确"的版本(假设你指的是cctype标题中的版本?),你可以使用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';
}

编辑:我不得不说,我很困惑为什么你会专门选择那个版本,而不是得到一个模棱两可的错误。但我猜不出你的代码到底发生了什么变化。。。

对我有用。也许你忘了包含<algorithm>

它应该这样工作:

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
   string ans;
    cin>>ans;
    std::transform(ans.begin(), ans.end(), ans.begin(), ::tolower);
   cout << ans;
   return 0;
}