找不到与"std::transform..." 的匹配项
Could not find a match for 'std::transform..."
我有一个奇怪的错误,代码以前可以工作,但过了一段时间就停止了编译。错误为:
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;
}
相关文章:
- 使用std::transform将一个范围的元素添加到另一个范围中
- 使用 std::transform 将向量向量 (a) 添加到另一个向量向量 (b)
- std::transform 将一个结构数组复制到另一个结构数组
- 如何正确使用 std::transform 和 std::mem_fn?
- 如何在 std::transform 中使用 std::p ow 作为一元函数?
- 将 std::transform 与 std::back_inserter 一起使用是否有效?
- 使用 std::transform 最终向量保持为空
- C++ 通过 std::transform 的 vector 索引获取向量
- 使用 std::transform 在 2d C 阵列上转换为 1d C 数组
- std::transform 可以替换为 std::累积吗?
- 使用 std ::transform 构造 std::vector.返回未命名结果的可能性?
- std::transform C++ 上的致命错误
- std::transform 操作到 --end() 和奇怪的输出
- 在 lambda(parameter) 中为 std::transform with std::sregex_token
- 如何使用 std::transform with container without push_back.
- std::transform with std::reference_wrapper
- std::transform 或 std::generate 在没有 ExecutionPolicy 的情况下是否可以并
- 如何使用for_each,transform,Iterator和lambda的某种组合将STD ::集合的字符串转换为小
- std::transform的最后一个参数
- 在 std::for_each 和 std::transform 中正确使用函子