当前位置:   article > 正文

C++面试题--string类的实现_c++ 面试写string 类的实现

c++ 面试写string 类的实现

c++的一个常见的面试题即写一个简单的string类,这个类中应该有着必要的构造函数、复制构造函数、赋值操作符、析构函数等功能,需要很好的处理内存的问题。

赋值操作符的多种写法:

1.一般的经典写法为(没有考虑异常处理):
这里还要注意的是:令赋值操作符返回一个reference to *this:详细见《effective c++》

String& String::operator=(const String& rhs) {
    if (this == &rhs) {
        return *this;
    }
    delete[]_data;
    _data = new char[rhs.size() + 1];
    strcpy(_data, rhs._data);
    return *this
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

这里在进行new的过程中可能会出现异常,因为其内存不够。

2. Copy and swap技术

void Swap(String rhs) {
        std::swap(_data, rhs._data);
    }
String& operator=(const String& rhs) {
        String temp(rhs);       //利用复制构造函数
        Swap(rhs);
        return *this;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

3.传值

    String& operator=(String rhs) {  //这里是传值,只是副本
        Swap(rhs);
        return *this;
    }
  • 1
  • 2
  • 3
  • 4

4.C++11的右值引用

    String& operator= (String&& rhs) {
        Swap(rhs);
        return *this;
    }
  • 1
  • 2
  • 3
  • 4

其余的感觉就没有什么难点了,完整代码如下:

//Date      : 2016.11.6
//Autore    : yqtao
/*
    string 类的实现
*/
#ifndef STRING_H
#define STRING_H
#include<iostream>
using namespace std;
class String {
private:
    char* _data;
public:
    String(const char* str = nullptr);
    String(const String& rhs) :_data(new char[rhs.size() + 1]) {
        strcpy(_data, rhs._data);
    }
    String& operator=(const String& rhs);
    ~String() {
        if (_data != nullptr) {
            delete[]_data;
            _data = nullptr;
        }

    }
    void Swap(String rhs) {
        std::swap(_data, rhs._data);
    }
    size_t size() const {
        return strlen(_data);
    }
private:
    //重载操作符
    char&  operator[](int index);
    friend ostream& operator<<(ostream &os, const String &rhs);
    friend istream& operator >> (istream &is, String &rhs);
    friend String operator+(const String& lhs, const String& rhs);
    friend String operator+=(String& lhs,const String& rhs);   //注意这里不能声明成const
    friend bool operator==(const String& lhs, const String& rhs);
    friend bool operator!=(const String& lhs, const String& rhs);
    friend bool operator>(const String &lhs, const String &rhs);
    friend bool operator>=(const String &lhs, const String &rhs);
    friend bool operator<(const String &lhs, const String &rhs);
    friend bool operator<=(const String &lhs, const String &rhs);
};
String::String(const char* str) {       //构造函数,默认的输入为空
    if (str == nullptr) {
        _data = new char[1];
        *_data = '\0';
    }
    else {
        _data = new char[strlen(str) + 1];
        strcpy(_data, str);
    }
}
//
String& String::operator=(const String& rhs) {
    if (this == &rhs) {
        return *this;
    }
    delete[]_data;
    _data = new char[rhs.size() + 1];
    strcpy(_data, rhs._data);
}
/*
    其他的写法:
    //2. copy and swap技术
    String& operator=(const String& rhs) {
        String temp(rhs);       //利用复制构造函数
        Swap(rhs);
        return *this;
    }
    //3. 传值方式
    String& operator=(String rhs) {
        Swap(rhs);
        return *this;
    }
    //4. c++11右值引用
    String& operator= (String&& rhs) {
        Swap(rhs);
        return *this;
    }
*/
char& String::operator[](int index) {
    return _data[index];
}
ostream& operator<<(ostream &os, const String &rhs) {
    os << rhs._data;
    return os;
}
/*
//注意这里可能会出错,如果有大神这道什么原因,请指教
istream& operator >> (istream &is, String &rhs) {     
    String tmp;
    is >> tmp._data;
    rhs = tmp;
    return is;
}
*/
//重载+
String operator+(const String& lhs, const String& rhs) {
    String res;
    int len = lhs.size() + rhs.size();
    res._data = new char[len + 1];
    strcpy(res._data, lhs._data);
    strcat(res._data, rhs._data);
    return res;
}
//重载+=
String operator+=(String& lhs, const String& rhs) {
    lhs = lhs + rhs;    //利用加法即可完成 
    return lhs;
}
//重载==
bool operator==(const String& lhs, const String& rhs) {
    if (strcmp(lhs._data, rhs._data) == 0) return true;
    return false;
}
bool operator!=(const String& lhs, const String& rhs) {
    if (strcmp(lhs._data, rhs._data) == 0) return false;
    return true;
}
//重载>函数  
bool operator>(const String &lhs, const String &rhs){
    if (strcmp(lhs._data, rhs._data) > 0)
        return true;
    return false;
}
bool operator<(const String &lhs, const String &rhs) {
    if (strcmp(lhs._data, rhs._data) < 0)
        return true;
    return false;
}
//重载<=函数  
bool operator<=(const String &lhs, const String &rhs){
    if (strcmp(lhs._data, rhs._data) <= 0)
        return true;
    return false;
}

//重载>=函数  
bool operator>=(const String &lhs, const String &rhs){
    if (strcmp(lhs._data, rhs._data) >= 0)
        return true;
    return false;
}
#endif // !STRING_H
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147

测试:

#include "String.h"
int main() {
    String s;
    cout << s<<endl;
    String s1 = "test";
    cout << s1 << endl;
    String s2(s1);
    cout << s2 << endl;
    s = s2;
    cout << s << endl;
    /*
    String s3;
    cout << "Please input a string" << endl;
    cin >> s3;
    cout << s3 << endl;
    */
    //
    String s4 = "hello";
    String s5 = "world";
    String s6 = s4 + s5;
    cout << s6 << endl;
    s6 += s4;
    cout << s6 << endl;
    cout << (s6 == s4) << endl;
    String s7(s6);
    cout << (s6 == s7) << endl;
    //
    cout << (s4 > s5) << endl;
    cout << (s6 > s5) << endl;  //s6=helloworld,s5=world,因此此句应为false
    cout << (s6 > s4) << endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

遇到的问题:在重载>>时出现了内存错误.
如果你知道什么原因,Please let me know,thank you

istream& operator >> (istream &is, String &rhs) {
    String tmp;      //开辟一个新的类,用于赋值
    is >> tmp._data;
    rhs = tmp;
    return is;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

完整代码测试在我的github:
https://github.com/yqtaowhu/DataStructureAndAlgorithm

参考资料:
1. 《Effective c++》
2. http://blog.csdn.net/bao2516090/article/details/47153331

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/868830
推荐阅读
相关标签
  

闽ICP备14008679号