“035” 绝对更有意义。 最佳答案 C 字符串字面量(例如 "100")将自身作为指-6ren">
gpt4 book ai didi

c++ - C++ std::string::compare() 与字符串运算符之间的不一致

转载 作者:行者123 更新时间:2023-12-01 14:15:02 24 4
gpt4 key购买 nike

给定下面的一段 C++ 代码

cout<<("100">"035")<<"\n";
cout<<("100"<"035")<<"\n";
string str = "100";
cout<<str.compare("035");

这段代码的输出是

0
1
1

这意味着运算符(operator)“100”<“035”但比较功能“100”>“035”。这两者有任何已知的实现差异吗?

附言“100” > “035” 绝对更有意义。

最佳答案

C 字符串字面量(例如 "100")将自身作为指针进行比较。

std::string comparison compare content lexicography.

如果您想要一致的结果:

using namespace std::string_literals;
std::cout << ("100"s > "035"s)<<"\n";
std::cout << ("100"s < "035"s)<<"\n";
std::string str = "100"s;
std::cout << str.compare("035");

“100”s“等同于”std::string("100")

关于c++ - C++ std::string::compare() 与字符串运算符之间的不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61300156/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com