C++ string 较为完整的实现

平常在面试中经常会有要求写string 内部实现.现在总结如下

string 头文件:

#ifndef _STRING_H_
#define _STRING_H_
 
#include <iostream>
 
using namespace std;
 
class String
{
public:
    String();
    String(char *str);
    String(const String& other);
    ~String();
 
    String& operator=(char *str);
    String& operator=(const String& other);
 
    char& operator[](unsigned int index);
    const char& operator[](unsigned int index) const;
 
    friend String operator+(const String& s1, const String& s2);
 
    String& operator+=(const String& s);
 
    friend ostream& operator<<(ostream& out, const String& s);
    friend istream& operator>>(istream& in, String& s);
 
    void Display();
 
private:
    char *str_;
};
 
#endif

 

类中具体实现:

 
#include <iostream>
#include <string.h>
#include "String.h"
 
using namespace std;
 
String::String()
{
    str_ = new char[1];

    str_[0] = '\0';
    cout << "default construct String!" << endl;
}
 
String::String(char *str)
{
    cout << "construct String!" << endl;
 
    int len = strlen(str) + 1;
    str_ = new char[len];
    memset(str_, 0, len);
    strcpy(str_, str)
}
 
String::String(const String& other)
{
    int len = strlen(other.str_) + 1;
    str_ = new char[len];
    memset(str_, 0, len);
    strcpy(str_, other.str_);
}
 
String& String::operator=(const String& other)
{
    if(this == &other)
    {
        return *this;
    }
    
    int len = strlen(other.str_) + 1;
    delete [] str_;
    str_ = new char[len];
    memset(str_, 0, len);
    strcpy(str_, other.str_);
 
    return *this;
}
 
String& String::operator=(char *str)
{
    delete [] str_;
    int len = strlen(str) + 1;
    str_ = new char[len];
    memset(str_, 0, len);
    strcpy(str_, str);
 
    return *this;
}
 
char& String::operator[](unsigned int index)
{
    //return str_[index];
    return const_cast<char&>(static_cast<const String&>(*this)[index]);
}
 
const char& String::operator[](unsigned int index) const
{
    return str_[index];
}
 
String operator+(const String& s1, const String& s2)
{
#if 0
    int len = strlen(s1.str_) + strlen(s2.str_) + 1;
    char *newptr = new char[len];
    memset(newptr, 0, len);
    strcpy(newptr, s1.str_);
    strcat(newptr, s2.str_);
    String tmp(newptr);
#endif
 
    String tmp(s1);
 
    tmp += s2;
 
    return tmp;
}
 
String& String::operator+=(const String& s)
{
    int len = strlen(s.str_) + strlen(str_) + 1;
 
    char *newptr = new char[len];
    memset(newptr, 0, len);
    strcpy(newptr, str_);
    strcat(newptr, s.str_);
    String tmp(newptr);
    
    delete [] str_;
    str_ = new char[len];
    strcpy(str_, newptr);
 
    return *this;
}
 
ostream& operator<<(ostream& out, const String& s)
{
    out << s.str_;
    return out;
}
 
istream& operator>>(istream& in, String& s)
{
    char buffer[4096];
 
    in >> buffer;
    s.str_ = buffer;
 
    return in;
}
 
String::~String()
{
    cout << "destroy String!" << endl;
}
 
void String::Display()
{
    cout << "str = " << str_ << endl;
}

原文地址:https://blog.csdn.net/zhengqijun_/article/details/55106090

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值