"transform"不是"std"的成员。

5

这是我的代码

#include "pch.h"
#include <iostream>
using namespace std;

int main()
{
    // su is the string which is converted to lowercase
    std::wstring su = L"HeLLo WoRld";

    // using transform() function and ::toupper in STL
    std::transform(su.begin(), su.end(), su.begin(), ::tolower);
    std::cout << su << std::endl;
    return 0;
}

它会产生以下编译错误。
1>c:\users\root\source\repos\consoleapplication7\consoleapplication7\consoleapplication7.cpp(14): error C2039: 'transform': is not a member of 'std'
1>c:\users\root\source\repos\consoleapplication7\consoleapplication7\consoleapplication7.cpp(14): error C3861: 'transform': identifier not found
1>c:\users\root\source\repos\consoleapplication7\consoleapplication7\consoleapplication7.cpp(15): error C2679: binary '<<': no operator found which takes a right-hand operand of type 'std::wstring' (or there is no acceptable conversion)
1>    0 Warning(s)
1>    3 Error(s)

我在这里做错了什么?


11
std::transform 声明在头文件 <algorithm> 中,你需要包含该头文件 (#include)。 - Yksisarvinen
4
使用 std::wcout 输出 std::wstring - Thomas
1
另外,为了实现const正确性,请尽可能(且恰当地)使用cbegin()和cend()。 - Jupiter
1个回答

20

您需要包含头文件。

添加:

#include <algorithm>

请使用 using namespace std;std::transform - Udesh
我也遇到了std::ranges::transform的问题 - 它并没有在ranges头文件中定义,而是在algorithm头文件中定义。 - Josie Thompson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接