【发布时间】:2021-03-16 20:15:29
【问题描述】:
string::compare() 的重载形式之一为
int compare (size_t pos, size_t len, const string& str) const;
现在考虑以下两种情况:
场景 1:
string s1="steven", s2="steve";
cout<<s1.compare(0, 5,s2);
场景 2:
string s1="steven", s2="stevec";
cout<<s1.compare(0, 5,s2);
第一种情况下的 O/P 为 0(预期),但在第二种情况下产生 -1(意外)。 fn 调用转换为 比较 s1 的前 5 个字符和 s2 的那些。那么,输出为什么会受到 s2 的第 6 个字符的影响。有人可以解释一下这种实现背后的原因吗?一个标准库 fn。
【问题讨论】: