[c++]String类的实现(输入输出赋值比较大小运算符重载)

本文档展示了如何实现一个名为String的类,该类使用动态字符数组存储字符串,并实现了字符串连接、赋值、比较、子串获取、字符访问、输入输出等操作。同时,还为该类重载了各种比较和流操作符,如+、+=、>、>=、<、<=、!=、==以及>>和<<。
摘要由CSDN通过智能技术生成

实现一个处理字符串的类String。它用一个动态的字符数组保存一个字符串。实现的功能有:字符串连接(+和+=),字符串赋值(=),字符串的比较(>, >=, < , <=, !=, = =),取字符串的一个子串,访问字符串中的某一个字符,字符串的输入输出(>>和<<)。

//String.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS
# include<iostream>
using namespace std;
class String
{
    friend String operator+(const String& s1, const String& s2);
    friend ostream& operator<<(ostream& os, const String& obj);
    friend istream& operator>>(istream& is, String& obj);
    friend bool operator>(const String& s1, const String& s2);
    friend bool operator>=(const String& s1, const String& s2);
    friend bool operator==(const String& s1, const String& s2);
    friend bool operator!=(const String& s1, const String& s2);
    friend bool operator<(const String& s1, const String& s2);
    friend bool operator<=(const String& s1, const String& s2);
private:
    int len;
    char* data;
public:
    String(const char* s = "");
    String(const String& other);
    String& operator+=(const String& other);
    String& operator=(const String& other);
    String operator()(int start, int end);
    char& operator[](int index);
    ~String();
};
//String.cpp
#include"String.h"
String::String(const char* s)
{
    data = new char[strlen(s) + 1];
    len = strlen(s);
    strcpy(data, s);
    data[len] = '\0';
}

String::String(const String& other)
{
    len = other.len;
    data = new char[len + 1];
    strcpy(data, other.data);
}

String& String::operator+=(const String& other)
{
    char* temp = data;
    len += other.len;
    data = new char[len + 1];
    strcpy(data, temp);
    strcat(data, other.data);
    return *this;
    // TODO: 在此处插入 return 语句
}

String& String::operator=(const String& other)
{
    if (data != NULL)
    {
        delete[]data;
        data = NULL;
    }
    data = new char[other.len + 1];
    len = other.len;
    strcpy(data, other.data);
    return *this;
    // TODO: 在此处插入 return 语句
}

String String::operator()(int start, int end)
{
    String temp;
    temp.data = new char[end - start + 1];
    temp.len = end - start;
    int j = 0;
    for (int i = start; i < end; i++)
    {
        temp.data[j] = data[i];
        j++;
    }
    temp.data[end - start] = '\0';
    return temp;
}

char& String::operator[](int index)
{
    return data[index];
    // TODO: 在此处插入 return 语句
}

String::~String()
{
    delete[]data;
}

String operator+(const String& s1, const String& s2)
{
    char* p = new char[s1.len + s2.len + 1];
    strcpy(p, s1.data);
    strcat(p, s2.data);
    return String(p);
}

ostream& operator<<(ostream& os, const String& obj)
{
    os << obj.data;
    return os;
    // TODO: 在此处插入 return 语句
}

istream& operator>>(istream& is, String& obj)
{
    obj.data = new char[512];//这里大小定的512
    is >> obj.data;
    obj.len = strlen(obj.data);
    return is;
    // TODO: 在此处插入 return 语句
}

bool operator>(const String& s1, const String& s2)
{
    if (strcmp(s1.data, s2.data) > 0) return true;
    else return false;
}

bool operator>=(const String& s1, const String& s2)
{
    if (strcmp(s1.data, s2.data) >= 0) return true;
    else return false;
}

bool operator==(const String& s1, const String& s2)
{
    if (strcmp(s1.data, s2.data) == 0) return true;
    else return false;
}

bool operator!=(const String& s1, const String& s2)
{
    if (strcmp(s1.data, s2.data) != 0) return true;
    else return false;
}

bool operator<(const String& s1, const String& s2)
{
    if (strcmp(s1.data, s2.data) < 0) return true;
    else return false;
}

bool operator<=(const String& s1, const String& s2)
{
    if (strcmp(s1.data, s2.data) <= 0) return true;
    else return false;
}

主函数部分就没有放上去啦。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值