From e22d05f823c365e1bf044bb678e12de0d1e3eafd Mon Sep 17 00:00:00 2001 From: lensferno Date: Thu, 7 Apr 2022 08:19:25 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=8A=A8=E6=80=81=E5=88=86?= =?UTF-8?q?=E9=85=8D=E7=89=88=E7=9A=84=E9=97=AE=E9=A2=98=20=E6=89=8B?= =?UTF-8?q?=E5=8A=A8=E5=88=A4=E6=96=AD=E5=AD=97=E7=AC=A6=E4=B8=B2=E5=A4=A7?= =?UTF-8?q?=E5=B0=8F=E7=9A=84=E6=97=B6=E5=80=99=E4=BC=9A=E6=9C=89=E9=97=AE?= =?UTF-8?q?=E9=A2=98...=20=E8=BF=98=E6=98=AF=E5=BE=97=E8=A6=81=E9=9D=A0?= =?UTF-8?q?=E5=BA=93=E5=87=BD=E6=95=B0...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- work20220401/neoString/CString.cpp | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/work20220401/neoString/CString.cpp b/work20220401/neoString/CString.cpp index 33a37c1..00811e4 100644 --- a/work20220401/neoString/CString.cpp +++ b/work20220401/neoString/CString.cpp @@ -145,7 +145,7 @@ CString &CString::operator+=(const CString &source) { } /** - * 按照字典顺序逐字对比 + * 按照字典顺序对比 */ bool CString::operator>(const CString &compareString) const { // 首先判断两CString对象data成员是否为空指针,空直接返回false @@ -158,14 +158,8 @@ bool CString::operator>(const CString &compareString) const { return length > compareString.length; } - // 以上情况都不符合时才进行逐字比较 - int compareLength = std::min(compareString.length, length); - for (int i = 0; i < compareLength; ++i) { - if (data[i] > compareString.data[i]) { - return true; - } - } - return false; + // 以上情况都不符合时才进行比较 + return strcoll(this->data, compareString.data) > 0; } std::ostream &operator<<(std::ostream &outputStream, const CString &source) { @@ -173,6 +167,8 @@ std::ostream &operator<<(std::ostream &outputStream, const CString &source) { } std::istream &operator>>(std::istream &inputStream, CString &source) { + // 使用istream.get(char*, int)可以限制读入的长度,防止输入超长的字符串导致溢出,但是空格也会被读入 + // 手动分割太麻烦,因此只好先用这种方法了 inputStream >> source.data; source.length = (int) strlen(source.data);