本文介绍了C ++-字符串类的实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现字符串类.这是我所做的:

I'm trying to implement string class. Here is what I have done:

#include <iostream>
#include <cstring>

using namespace std;

class MyString{
    private:
    char * content;
    int length;
    public:
    MyString ();
    MyString ( const char * );
    ~MyString ();
    MyString ( const MyString & );
    void print ( void );
    void operator = ( const MyString );
};

MyString :: MyString () {
    content = 0;
    length = 0;
}

MyString :: MyString(const char *n) {
    length = strlen (n);
    content = new char [ length ];
    for ( int i = 0 ; i < length ; i++ ){
    content [i] = n [i];
    }
    content [length] = '\0';
    }


MyString :: ~ MyString () {
    delete [] content;
    content = 0;
}


MyString :: MyString ( const MyString & x ) {
    length = x.length;
    content = new char [length];
    for( int i = 0 ; i < length ; i++ ){
    content [i] = x.content [i];
    }
    content [length] = '\0';
}

void MyString :: print( void ) {
    cout <<""<< content << endl;
}

void MyString :: operator = ( const MyString x ) {
    length = x.length;
    content = new char [length];
    for( int i = 0 ; i < length ; i++ ){
    content [i] = x.content [i];
    }
    content [length] = '\0';
}

int main() {
    MyString word1 ("stackoverflow");
    MyString word2;
    word2 = word1;
    word1.print();
    word2.print();
}

我编译了它,这就是我得到的:

I compiled it and this is what I get:

stackoverflow

stackoverflow

stackoverflow

stackoverflow

进程返回0(0x0)执行时间:0.050 s按任意键继续.

Process returned 0 (0x0) execution time : 0.050 sPress any key to continue.

尽管根据上述结果看起来正确,但我想知道它是否正确?我不太熟悉C风格的字符串,所以我很担心例如关于行:

Although it looks correct according to result above, I wonder is it really correct? I'm not so familiar with C-style strings so I'm concernedfor example about line:

content [length] = '\0';

由于C样式字符串的末尾有空终止符,因此我想终止数组,但这是正确的方法吗?我使用了动态内存分配,我也想知道我是否正确释放了资源?是否存在一些内存泄漏?预先感谢.

Since C-style strings has null terminator at end, I wanted to terminate my array but is this correct way to do it?I used dynamic memory allocation and I also wonder did I free resources properly?Are there some memory leaks?Thanks in advance.

我还重载了opeartor +(我想加入"MyStrings"),这是代码:

I also overloaded opeartor + (I want to join "MyStrings"), here is code:

MyString MyString :: operator + ( const MyString & x ){
    MyString temp;
    temp.length = x.length + length;
    temp.content = new char [ temp.length +  1 ];
    int i = 0, j = 0;
    while ( i < temp.length ) {
    if (i < length ) {
    temp.content [i] = content [i];
    }
    else {
    temp.content [i] = x.content [j];
    j ++;
    }
    i ++;
    }
    temp.content [ temp.length ] = '\0';
    return temp;
    }

这是主程序:

int main()
   {
   MyString word1 ( "stack" );
   MyString word2 ( "overflow" );
   MyString word3 = word1 + word2;
   word3.print();
   word3 = word2 + word1;
   word3.print();
   }

这是结果:

stackoverflow

stackoverflow

溢出堆栈

进程返回0(0x0)执行时间:0.040 s按任意键继续.

Process returned 0 (0x0) execution time : 0.040 sPress any key to continue.

我希望这段代码没有问题:)

I hope there are no problems with this code :)

这是使用for循环而不是while的+运算符的实现:

Here is implementation of + operator using for loops, instead of while:

MyString MyString :: operator + (const MyString & x){
    MyString temp;
    temp.length = x.length + length;
    temp.content = new char [temp.length+1];
    for( int i = 0 ; i < length ; i++ ){
    temp.content[i] = content[i];
    }
    for( int i = length , j = 0 ; i <temp.length ; i++, j++){
    temp.content[i] = x.content[j];
    }
    content[temp.length] = '\0';
    return temp;
}

现在可能会更好,因为没有if:)

It's maybe better now because there is no if :)

推荐答案

您正试图为 content [length] 分配一个值,但是您没有为 content分配足够的内存[长度] 进行访问.如果 length == 10 ,则可以通过 content [9] 访问 content [0] ,但不能访问 content [10].

You are trying to assign content[length] a value, but you haven't allocated enough memory for content[length] to be accessed. If length == 10, then you can access content[0] thru content[9], but not content[10].

当然可以通过从两个构造函数中删除行 content [length] = \ 0 来解决此问题,或者如果要添加 \ 0 ,则应增加 length 的值乘以 1 .

This can be fixed of course by removing the line content[length] = \0 from both constructors, or if you want to append \0 you should increase the value of length by 1.

您是否考虑过仅在内部使用 std :: string ?

Have you considered just using std::string internally?

@Thane Plummer首先在评论中指出了这一点!

@Thane Plummer was first to point this out in the comments!

这篇关于C ++-字符串类的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 04:54