自己实现一个C++字符串类,具体的实现代码
时间: 2023-08-31 12:38:14 浏览: 114
### 回答1:
可以参考以下代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DEFAULT_SIZE 128
typedef struct {
char *data;
size_t length;
size_t capacity;
} String;
String *string_new(void) {
String *self = malloc(sizeof(String));
if (!self) {
return NULL;
}
self->capacity = DEFAULT_SIZE;
self->data = malloc(self->capacity);
if (!self->data) {
free(self);
return NULL;
}
self->length = 0;
self->data[0] = '\0';
return self;
}
void string_free(String *self) {
if (self) {
if (self->data) {
free(self->data);
}
free(self);
}
}
int string_resize(String *self, size_t new_capacity) {
if (new_capacity <= self->capacity) {
return 0;
}
char *new_data = realloc(self->data, new_capacity);
if (!new_data) {
return -1;
}
self->data = new_data;
self->capacity = new_capacity;
return 0;
}
int string_append_c(String *self, char c) {
if (self->length + 1 >= self->capacity) {
if (string_resize(self, self->capacity * 2)) {
return -1;
}
}
self->data[self->length] = c;
self->length++;
self->data[self->length] = '\0';
return 0;
}
int string_append_s(String *self, const char *s) {
size_t s_length = strlen(s);
if (self->length + s_length >= self->capacity) {
if (string_resize(self, self->capacity + s_length)) {
return -1;
}
}
strcat(self->data, s);
self->length += s_length;
return 0;
}
int string_printf(String *self, const char *format, ...) {
va_list args;
va_start(args, format);
int result = vsnprintf(self->data + self->length, self->capacity - self->length, format, args);
va_end(args);
if (result < 0) {
return result;
}
self->length += result;
if ((size_t)result >= self->capacity - self->length) {
if (string_resize(self, self->capacity * 2)) {
return -1;
}
va_start(args, format);
result = vsnprintf(self->data + self->length, self->capacity - self->length, format, args);
va_end(args);
}
### 回答2:
C语言中没有现成的字符串类,但可以通过自定义结构体和相关函数实现一个字符串类。
首先,定义一个结构体来表示字符串类:
```c
typedef struct {
char* data; // 字符串内容
int length; // 字符串长度
} String;
```
然后,实现相关操作函数:
1. 创建字符串:
```c
String createString(const char* str) {
String newString;
newString.length = strlen(str);
newString.data = (char*)malloc((newString.length + 1) * sizeof(char));
strcpy(newString.data, str);
return newString;
}
```
2. 销毁字符串:
```c
void destroyString(String* str) {
free(str->data);
}
```
3. 字符串复制:
```c
void copyString(String* dest, const String* src) {
dest->length = src->length;
dest->data = (char*)malloc((src->length + 1) * sizeof(char));
strcpy(dest->data, src->data);
}
```
4. 字符串连接:
```c
void concatString(String* dest, const String* src) {
int totalLength = dest->length + src->length;
dest->data = (char*)realloc(dest->data, (totalLength + 1) * sizeof(char));
strcat(dest->data, src->data);
dest->length = totalLength;
}
```
5. 字符串比较:
```c
int compareString(const String* str1, const String* str2) {
return strcmp(str1->data, str2->data);
}
```
6. 获取字符串长度:
```c
int getStringLength(const String* str) {
return str->length;
}
```
这样,我们就实现了一个简单的C字符串类。使用时,可以使用类似于以下的方式操作字符串:
```c
String str1 = createString("Hello");
String str2 = createString("World");
concatString(&str1, &str2);
printf("%s\n", str1.data);
destroyString(&str1);
destroyString(&str2);
```
阅读全文