C++实现简单的string类

#ifndef _MY_STRING_
#define _My_STRING_

class MyString
{
public:
    MyString();
    MyString(const char* buf);//带参数构造
    MyString(const MyString& other);//拷贝构造

    ~MyString();//析构


    int Len()const;//长度
    char* c_str()const;//字符串

    MyString operator+(const MyString& other);//重构“+”,用于字符串拼接;
    MyString& operator=(const MyString& other);//重构“=”,用于字符串赋值;
    bool operator==(const MyString& other);//重构“==”,判断字符串相等

    char* data;        //字符串
    int data_len;//长度
};

#endif

 

 

#include "stdafx.h"

#include "MyString.h"
#include <iostream>

using namespace std;

MyString::MyString()
{
    data_len = 0;
    data = nullptr;
}

MyString::MyString(const char* buf)
{
    if (buf == nullptr)
    {
        data_len = 0;
        data = new char[1];
        strcpy(data, "");
    }
    else
    {
        data_len = strlen(buf);//获得长度
        data = new char[data_len + 1];//申请内存    
        strcpy(data, buf);//拷贝数据
    }
}

MyString::MyString(const MyString& other)
{
    data_len = strlen(other.data);//获得长度
    data = new char[data_len + 1];//申请内存    
    strcpy(data, other.data);//拷贝数据
}

MyString::~MyString()
{
    data_len = 0;
    if (data != nullptr)
    {
        delete[]data;
        data = nullptr;
    }
}

int MyString::Len() const
{
    return data_len;
}

char* MyString::c_str() const
{
    return data;
}

MyString MyString::operator+(const MyString& other)
{

    if (other.data == nullptr)
    {
        return *this;
    }
    else if (this->data == nullptr)
    {
        data_len = strlen(other.data);//获得长度
        data = new char[data_len + 1];//申请内存    
        strcpy(data, other.data);//拷贝数据
        return *this;
    }
    MyString Temp;
    Temp.data_len = this->data_len + other.data_len;
    Temp.data = new char[Temp.data_len + 1];
    strcpy(Temp.data, this->data);
    strcat(Temp.data, other.data);
    return Temp;
}

MyString& MyString::operator=(const MyString& other)
{
    if (other.data == nullptr)
    {
        return *this;
    }
    else if (&other == this)
    {
        return *this;
    }

    delete[] data;
    data = nullptr;
    data_len = other.data_len;
    data = new char[other.data_len + 1];
    strcpy(data, other.data);
    return    *this;
}

bool MyString::operator==(const MyString& other)
{
    if (this->data_len != other.data_len)
    {
        return false;
    }
    else
    {
        return strcmp(this->data, other.data) ? false : true;
    }
}

 

 

 

 

// MyString.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "MyString.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{

    MyString test_1("haha_1");
    cout << test_1.c_str() << endl;

    MyString test_2(test_1);
    cout << test_2.c_str() << endl;

    MyString test_3 = test_1 + test_2;
    cout << test_3.c_str() << endl;

    MyString test_4;
    test_4 = test_3;
    cout << test_4.c_str() << endl;

    if (test_1 == test_2)
    {
        cout << "same_char"<< endl;
    }
    else
    {
        cout << "not_char" << endl;
    }

    char a;
    a = getchar();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中的string是一个用于处理字符串的标准库。它提供了一系列成员函数和操作符重载,使得字符串的操作更加方便和高效。 C++中的string位于命名空间std中,因此在使用之前需要包含头文件< string >。 下面是一个简单的示例,展示了如何使用string来创建、初始化和操作字符串: ```cpp #include <iostream> #include <string> int main() { // 创建一个空字符串 std::string str; // 初始化字符串 std::string greeting = "Hello, world!"; // 获取字符串长度 int length = greeting.length(); std::cout << "Length: " << length << std::endl; // 连接字符串 std::string name = "Alice"; std::string message = greeting + " My name is " + name; std::cout << "Message: " << message << std::endl; // 获取子串 std::string substring = message.substr(7, 5); std::cout << "Substring: " << substring << std::endl; // 查找子串 size_t position = message.find("world"); if (position != std::string::npos) { std::cout << "Found at position: " << position << std::endl; } else { std::cout << "Not found" << std::endl; } return 0; } ``` 上述示例中,我们首先创建了一个空字符串`str`,然后使用赋值运算符将字符串"Hello, world!"赋给了变量`greeting`。接着,我们使用`length()`函数获取了字符串的长度,并使用`+`运算符将多个字符串连接起来形成新的字符串。我们还使用`substr()`函数获取了字符串的子串,并使用`find()`函数查找了子串在原字符串中的位置。 除了上述示例中的操作,string还提供了许多其他有用的成员函数和操作符重载,如插入、删除、替换、比较等。你可以参考C++的官方文档或其他相关资料来了解更多关于string的详细信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值