解决动态分配版的问题

手动判断字符串大小的时候会有问题...
还是得要靠库函数...
main
lensfrex 3 years ago
parent ef831c6a0e
commit e22d05f823
Signed by: lensfrex
GPG Key ID: 0F69A0A2FBEE98A0
  1. 14
      work20220401/neoString/CString.cpp

@ -145,7 +145,7 @@ CString &CString::operator+=(const CString &source) {
} }
/** /**
* *
*/ */
bool CString::operator>(const CString &compareString) const { bool CString::operator>(const CString &compareString) const {
// 首先判断两CString对象data成员是否为空指针,空直接返回false // 首先判断两CString对象data成员是否为空指针,空直接返回false
@ -158,14 +158,8 @@ bool CString::operator>(const CString &compareString) const {
return length > compareString.length; return length > compareString.length;
} }
// 以上情况都不符合时才进行逐字比较 // 以上情况都不符合时才进行比较
int compareLength = std::min(compareString.length, length); return strcoll(this->data, compareString.data) > 0;
for (int i = 0; i < compareLength; ++i) {
if (data[i] > compareString.data[i]) {
return true;
}
}
return false;
} }
std::ostream &operator<<(std::ostream &outputStream, const CString &source) { 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) { std::istream &operator>>(std::istream &inputStream, CString &source) {
// 使用istream.get(char*, int)可以限制读入的长度,防止输入超长的字符串导致溢出,但是空格也会被读入
// 手动分割太麻烦,因此只好先用这种方法了
inputStream >> source.data; inputStream >> source.data;
source.length = (int) strlen(source.data); source.length = (int) strlen(source.data);

Loading…
Cancel
Save