【发布时间】:2013-11-09 13:52:05
【问题描述】:
我试着了解如何使用std::tolower...
#include <iostream>
#include <string>
#include <algorithm>
#include <locale>
int main()
{
std::string test = "Hello World";
std::locale loc;
for (auto &c : test)
{
c = std::tolower(c, loc);
}
std::transform(test.begin(), test.end(), test.begin(), ::tolower); // 1) OK
std::transform(test.begin(), test.end(), test.begin(), std::tolower); // 2) Cryptic compile error
std::transform(test.begin(), test.end(), test.begin(), static_cast<int(*)(int)>(std::tolower)); // 3) Cryptic compile error. Seems OK with other compilers though
return 0;
}
所以:
- 为什么
::tolower版本有效? - 为什么
std::tolower在 std::transform 中不起作用? -
static_cast<int(*)(int)>(std::tolower))真正想要做什么?为什么 它是否适用于 GCC 而不适用于 Visual Studio 2013? - 那我如何在 std::transform 中使用
std::lower和 Visual Studio 2013?
【问题讨论】:
-
您是否尝试过包含
<cctype>标头which is wherestd::toloweris actually defined? -
oups, Oo' ^^' 但是,为什么它与 GCC 一起工作?
-
@Korchkidu,因为使用 GCC,您包含的其他标头之一恰好包含
<cctype>。永远不要将“它恰好与一个编译器一起工作”误认为意味着代码实际上是正确的,尤其是对于内部包含其他库头文件的头文件。 -
@JonathanWakely:谢谢,确实很清楚。
标签: c++ visual-studio c++11