创建字符串数据串起
Creating a String Data Stucture
我对我的字符串类分配非常困惑。我基本上是在制作字符串数据结构,但不使用string data type
。它需要三个数据成员:价值,长度和容量。我做了。
#pragma once
#include <iostream>
#include <string>
using namespace std;
class Str {
private:
int length;
int capacity;
char* value;
还假设有8个类方法:成长,最小,差异,getchararrsize,coppy,concatenate,concateNate,concare和Print和print。我得到了成长,最小,差异和getchararrsize。他们看起来像这个
void grow() {
char *temp = value;
capacity *= 2;
value = new char[capacity];
for (int i = 0; i < length; i++) {
value[i] = temp[i];
}
}
int min(int a, int b) {
if (a < b) return a;
else return b;
}
int diffrence(int a, int b) {
int d = 0;
if (a > b) d = a - b;
else if (b > a) d = b - a;
return d;
}
int getCharArrSize(char *v) {
int c = 0;
while (v[c] != ' ') {
c++;
}
return c;
}
现在,指示说生长用于复制和连接酸盐。那将如何工作?复制的说明是:覆盖字符串的数据带有s中包含的数据的数组。对于串联而为:将数据附加到存储在字符串中的数据。我也在标题文件中执行所有这些操作。测试文件看起来像这样:
#pragma once
#include "NewString.h"
#include <iostream>
#include <string>
using namespace std;
int main() {
Str s1("Hello ");
Str s2("World");
Str s3(", My ");
Str s4("Name ");
Str s5("is ");
Str s6("Chad!");
cout << s1.size() << endl;
Str s7;
s7.copy(s1);
s7.concatenate(s2);
s7.concatenate(s3);
s7.concatenate(s4);
s7.concatenate(s5);
s7.concatenate(s6);*/
s7.print();
cout << "nn";
Str s8("Hello World, My Name is Chad!");
if (s8.compare(s7) == 0) {
cout << "They Match!" << endl;
}
Str s9("I dont match....");
if (s9.compare(s8) != 0) {
cout << "I differ by " << s9.compare(s8) << " characters..." <<
endl;*/
}
}
我很困惑,不知道该如何进行。请帮助我。预先感谢
所以让我们开始查看grow((做什么:
void grow() {
char *temp = value;
capacity *= 2;
value = new char[capacity]; // Double capacity
for (int i = 0; i < length; i++) {
value[i] = temp[i]; // Copy all data from temp into value
}
}
因此,例如,如果此之前的速度为 ['a', 'b', 'c']
,现在将为 ['a', 'b', 'c', '', '', '']
。有意义吗?
现在的问题是:如何帮助我们复制和连接?让我们从复制开始。我们需要能够将参数中的字符串复制到调用该方法的字符串中。例如,调用str1.copy(str2)
后,str1
应与str2
相同。但这是问题所在:如果str2
的容量大于str1
的容量怎么办?我们不能简单地将str2
的内容复制到str1
中,因为它们不合适。因此:我们使用grow()
方法直到足够大。
void copy(const Str& other) {
// Grow buffer
while (this.capacity < other.capacity) {
this.grow();
}
// Copy contents of other
for (int charIndex = 0; charIndex < other.capacity; charIndex++) {
this.value[charIndex] = other.value[charIndex]
}
}
现在,我们可以轻松地将此知识扩展到concatenate
:我们要将str2
添加到str1
,但是str1
还不够大。要解决此问题,我们将grow()
调用直到我们大于或等于str1.capacity + str2.capacity
,然后将str2
的内容复制到str1
的末尾。
欢呼和好运!
- C++浮点数据类型和字符串数据类型无法子到模板函数中
- 有哪些有效的方法可以消除一组 100 万个字符串>重复数据?
- 具有多个模板的模板函数,用于特定数据类型(如字符串)?
- 用于存储由空格分隔的字符串的 C++/C 数据结构
- 将字符串与文件上的数据进行比较
- 如何在C++中将十六进制字符串转换为文本数据
- 如何解析表示树状数据结构的字符串
- 为什么字符串的 move() 会改变内存中底层数据的位置?
- 将收到的 usart uint8_t* 数据与常量字符串进行比较
- 使用从文件(stod、strtod、atof)中提取的数据C++从字符串转换为双精度.csv
- 如何避免将数据缓冲区的额外副本复制到字符串?
- C++ - 使用用户输入的字符串数据检查结构字符串数据(无限执行 while 循环)
- 在 MFC 中使用 strncpy() 复制字符串时截断的数据
- 利用输入文件中的数据(字符串和int)并在循环中利用
- 如何用 c++ 读取 txt 文件,如果我们不知道其中有多少个数据字符串,以及每行有多长?
- 将数据(字符串或整数)添加到 char 数组
- 从文本文件到实际十六进制的文本十六进制数据字符串
- 如何将流数据(字符串)解析为不同的数据文件
- 当绕过c++中的函数从目标进程的ExtTextOut复制数据字符串时,malloc会导致崩溃吗?
- 如何将数据(字符串、整数或枚举成员)与Gtk::ComboBoxText项关联?