[php面试题]C++面试题string类的实现例子

更新时间:2020-12-02    来源:php函数    手机版     字体:

【www.bbyears.com--php函数】

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);
}
这里在进行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;
    }
3.传值

String& operator=(String rhs) {  //这里是传值,只是副本
        Swap(rhs);
        return *this;
    }
4.C++11的右值引用

String& operator= (String&& rhs) {
        Swap(rhs);
        return *this;
    }
其余的感觉就没有什么难点了,完整代码如下:

//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
测试:

#include "String.h"
int main() {
    String s;
    cout << s<     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;
}
遇到的问题:在重载>>时出现了内存错误.如果你知道什么原因, Please let me know,thank you

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

iostream>

本文来源:http://www.bbyears.com/jiaocheng/113947.html