use*_*329 1 c++ libstdc++ language-lawyer
我对这个事实感到有点惊讶
#include <string>
#include <cassert>
#include <cstdio>
int main()
{
printf("Floating point format: %.7g\n",1.4);
std::string a("Hello, World/#");
std::string b("Hello, World 2");
assert(a>b);
assert(setlocale(LC_ALL,"sv_SE.UTF-8")!=NULL);
printf("New floating point format: %.7g\n",1.4);
assert(b<a);
}
Run Code Online (Sandbox Code Playgroud)
http://coliru.stacked-crooked.com/a/1b435636e4ff7161
该程序正常退出。结论,语言环境会影响 a 和 b 之间的比较。那是对的吗?这意味着当使用 std::string 作为 std::set 等中的键时,排序不变量会因当前语言环境的改变而被破坏,而没有自定义比较函数。
std::string 的 operator< 是否应该受当前语言环境的影响?
否。运营商std::string::compare内部使用。这反过来std::string::traits_type用于比较。std::string::traits_type是std::char_traits<char>与语言环境无关的。
结论,语言环境会影响 a 和 b 之间的比较。
无法得出这样的结论,因为断言之间的顺序保持不变。