setlocale() 是 C,而不是 C++。我隐约记得在 VC6 上看到两者之间存在干扰,但那是一个错误。通常,setlocale() 只影响 C 函数的行为。
在 C++ 中,本地化由 std::locale 类控制。默认情况下,区域设置敏感操作使用全局区域设置,这是通过默认构造一个区域设置对象获得的,并且可以使用 std::locale::global(const std::locale&) 设置。
用空字符串 (std::locale("")) 构造一个语言环境对象会创建一个与程序环境相对应的语言环境。
在程序启动时,全局语言环境是“C”或“经典”语言环境。要将全局语言环境设置为程序的环境语言环境(我猜这就是你要问的),你可以这样写:
std::locale::global(std::locale(""));
例如,我的区域设置当前设置为法语(加拿大)。运行这个:
int main(void)
{
std::cout << std::locale().name() << std::endl;
std::locale::global(std::locale(""));
std::cout << std::locale().name() << std::endl;
std::locale::global(std::locale("C"));
std::cout << std::locale().name() << std::endl;
return 0;
}
印刷:
C
French_Canada.1252
C