字符指针对象和相应的字符数组元素比较
Char pointer objects and respective char array element comparisons
我正在使用多种方法对自定义字符串类进行编程。问题是比较方法没有按我的预期工作。当两个 char 数组不同时,而不是什么都不做,if 条件仍然在我的主函数中进行。
当我使用 g++ 编译时没有给出任何错误。代码在语法上是正确的,但在逻辑上是错误的。我知道这一点,因为我可以为比较方法提供两个内容不同的字符数组,并且它们是否以这种方式不同并不重要,因为主函数将为"s8.compare(s7) == 1"运行 if 条件,无论比较方法中的结果是否不正确。
我将在下面发布整个代码。任何帮助将不胜感激。
字符串.h
class Str {
private:
char *value;
int length;
int capacity;
//Doubles the size of the string when called.
void growArray();
//If the two strings are uneven, get absolute value of difference in length.
int difference(int a, int b);
//Calculates the size of a character array, passed in as an argument
int getCharArrSize(const char *v);
public:
Str();
explicit Str(const char *STR);
void copy(Str s);
void concatenate(Str s);
bool compare(Str s);
void print();
};
//Str constructor
Str::Str() {
//Assign value, capacity, and length to any new Str object
value = new char[100];
capacity = 100;
length = 0;
}
//Pass STR object as a pointer to string object constructor
Str::Str(const char *STR) {
length = getCharArrSize(STR);
capacity = 100;
value = new char[capacity];
//Copy contents from STR to string object
for (int i = 0; i < length; i++)
value[i] = STR[i];
}
//Doubles the size of the string when called.
void Str::growArray() {
const char *tmp = value;
capacity *= 2;
value = new char[capacity];
for (int i = 0; i < length; i++)
value[i] = tmp[i];
}
//If the two strings are uneven, get absolute value of difference in length.
int Str::difference(int a, int b) {
int d = 0;
if (a > b) d = a - b;
else if (b > a) d = b - a;
return d;
}
//Calculates the size of a character array, passed in as an argument
int Str::getCharArrSize(const char *v) {
int c = 0;
while (v[c] != ' ') {
c++;
}
return c;
}
//Overwrites the data of the string array with the data contained in s
void Str::copy(Str s) {
//Check ability for empty string object to hold Str s contents
if (capacity > s.length) {
//Copy over each element until s length is reached
for (int i = 0; i < s.length ; i++)
value[i] = s.value[i];
//Set string object length to copy's size
length = getCharArrSize(value);
} else { growArray(); }
}
//Concatenate Str s onto string object
void Str::concatenate(Str s) {
//Check ability for string object to hold itself and concatenated chars
if (capacity > length + s.length) {
//Fill string object with s object until end of combined lengths if necessary
for (int i = 0; i < length + s.length; i++)
value[length + i] = s.value[i];
//Set length based on chars in concatenated string object
length = getCharArrSize(value);
} else { growArray(); }
}
//Compare each element in Str s against string for similarities
bool Str::compare(Str s) {
if (length == s.length) {
if (*value == *s.value) {
while ((*value != value[length]) && (*s.value != s.value[s.length])) {
value++;
s.value++;
}
return true;
} else return false;
} else {
difference(length, s.length);
}
}
//Print function
void Str::print() {
std::cout << value << std::endl;
}
主.cpp
#include"string.h"
int main() {
Str s1("Hello ");
Str s2("World");
Str s3(", my ");
Str s4("Name ");
Str s5("is ");
Str s6("Chad!");
Str s7;
s7.copy(s1);
s7.concatenate(s2);
s7.concatenate(s3);
s7.concatenate(s4);
s7.concatenate(s5);
s7.concatenate(s6);
s7.print();
std::cout << "nn";
Str s8("Hello World, My Name is Chad!");
if (s8.compare(s7) == 1) {
std::cout << "They Match!" << std::endl;
}
Str s9("I dont match....");
if (s9.compare(s8) == 0) {
std::cout << "I differ by " << s8.compare(s6) << " characters" << std::endl;
}
}
上面的代码返回一个看起来正确的结果,但是当我尝试检查 char 数组中的每个单独元素时,将 (s8.compare(s7) == 1) 更改为类似 (s8.compare(s5) == 1) 返回"它们匹配!",并且仅当它们的长度相同并且数组中的每个字符都匹配时才返回 true。
您的程序具有未定义的行为,因为Str::compare
在其中一个分支中没有return
语句。
bool Str::compare(Str s) {
if (length == s.length) {
...
} else {
// Missing return statement.
difference(length, s.length);
}
}
也许您想将该行更改为:
return (difference(length, s.length) == 0);
您的循环在没有比较的情况下运行。您比较 char 数组中的初始值,然后循环访问其余值而不进行比较。因此,每次初始值相等时,您都将返回 true。
在确定相同长度后,循环运行下方,然后比较每个字符。如果它们不相等,则该函数将返回 false。否则,该函数将返回 true。
bool Str::compare(Str s) {
if (length == s.length) {
while ((*value != value[length]) && (*s.value != s.value[s.length])) {
if (*value == *s.value) {
value++;
s.value++;
} else {
return false;//will return false as soon as a comparison is false
}
}
return true;
} else {
difference(length, s.length);
}
}
您还需要从差分函数返回一个布尔值。如果要从该函数返回 int,请在比较函数上切换到 int 返回,并使用 0 和 1 作为它们的布尔对应项。
相关文章:
- 对字符数组中的元素执行逐位操作
- 从字符数组的元素中减去'a'是什么意思
- 为什么在 c++ 中分配 char 数组元素时,分配的字符会被销毁?
- C++ 无法更改字符数组中的元素
- 如何在 2D 字符数组中打印 [x][y] 元素的地址?(C++)
- 在 c++ 中使用未知元素初始化的字符数组
- 字符指针对象和相应的字符数组元素比较
- 如何使用用户输入正确修改 2D 数组中的字符数组元素?专门用于电影院座位
- 类型转换问题:字符数组的元素转换为整数变量
- 指向较大的无符号字符数组的每个第 n 个元素的指针数组
- 如何获取字符**数组中的元素计数
- 将元素范围从字符数组提取到字符串中
- 检查字符数组的所有元素
- 以整数形式访问无符号字符数组的元素
- 如何提取并检查数组元素中的第一个字符是否为字母
- 如何编辑流式处理字符数组中的元素
- 如何使用本函数对两个字符数组进行元素相乘,并将相乘结果求和为int
- 在无符号字符数组和无符号字符向量中扫描特定元素的最快方法是什么?
- 将字符数组的元素转换为整数,并使用cout打印
- C++ - 2 个字符数组元素成短 int