class string

10 篇文章 0 订阅

字符串类的增删查改

class String
{
public:
    String(const char* str = "");
    ~String();
    String(const String& s);

    //const String& operator=(const String& s);
    const String& operator=(const String& s);
    void Swap(String& s);
    char* GetStr();
    size_t Len();
    size_t Capacity();

    // 增删查改 
    void PushBack(char ch);
    void PushBack(const char* str);
    void PopBack();
    void Insert(size_t pos, char ch);
    void Insert(size_t pos, const char* str);
    void Erase(size_t pos, size_t count);
    int Find(char ch) const;
    int Find(const char* str) const;
    char& operator[](size_t pos);
    bool operator==(const String& s) const;
    bool operator<(const String& s) const;
    bool operator<=(const String& s) const;
    bool operator>(const String& s) const;
    bool operator>=(const String& s) const;
    bool operator!=(const String& s)const;
    //void Expand(size_t n)

private:
    char* _str;
    size_t _len;
    size_t _capacity;
};

class_string.h

#pragma once

#include <iostream>
#include <string>
#include <assert.h>
using namespace std;

class String
{
public:
    String(const char* str = "")
        :_len(strlen(str)), _capacity(_len)
    {
        _str = (char*)malloc(_len + 1);
        strcpy(_str, str);
    }
    ~String()
    {
        free(_str);
    }
    String(const String& s)
        :_len(s._len), _capacity(s._capacity)
    {
        _str = (char*)malloc(s._capacity + 1);
        strcpy(_str, s._str);
    }

    const String& operator=(const String& s);
    void Swap(String& s);
    char* GetStr();
    size_t Len();
    size_t Capacity();

    // 增删查改 
    void PushBack(char ch);
    void PushBack(const char* str);
    void PopBack();
    void Insert(size_t pos, char ch);
    void Insert(size_t pos, const char* str);
    void Erase(size_t pos, size_t count);
    int Find(char ch) const;
    int Find(const char* str) const;
    char& operator[](size_t pos);
    bool operator==(const String& s) const;
    bool operator<(const String& s) const;
    bool operator<=(const String& s) const;
    bool operator>(const String& s) const;
    bool operator>=(const String& s) const;
    bool operator!=(const String& s)const;
    //void Expand(size_t n)

private:
    char* _str;
    size_t _len;
    size_t _capacity;
};

string.cpp

#define _CRT_SECURE_NO_WARNINGS 1

#include "class_string.h"

//const String::String& operator=(const String& s)
//{
//  if (_str != s._str)
//  {
//      String ss(s);
//      swap(_str, ss._str);
//      _len = ss._len;
//  }
//  return *this;
//}
const String& String::operator=(const String& s)
{
    if (_capacity < s._len)
    {
        _len = s._len;
        _str = (char*)realloc(_str, _len + 1);
        _capacity = _len;
    }
    strcpy(_str, s._str);
    return *this;
}
void String::Swap(String& s)
{
    swap(_str, s._str);
    swap(_len, s._len);
    swap(_capacity, s._capacity);
}
char* String::GetStr()
{
    return _str;
}
size_t String::Len()
{
    return _len;
}
size_t String::Capacity()
{
    return _capacity;
}

// 增删查改 
void String::PushBack(char ch)
{
    if (_len == _capacity)
    {
        _str = (char*)realloc(_str, ++_len + 1);
        _capacity++;
    }
    _str[_len] = '\0';
    _str[_len - 1] = ch;
}
void String::PushBack(const char* str)
{
    _len += strlen(str);
    if (_len > _capacity)
    {
        _str = (char*)realloc(_str, _len + 1);
        _capacity = _len;
    }
    //strcat(_str, str);
    char* dest = _str;
    while (*dest)dest++;
    while (*dest++ = *str++);
}
void String::PopBack()
{
    _str[--_len] = '\0';
}
void String::Insert(size_t pos, char ch)
{
    assert(pos <= _len);

    if (_len == _capacity)
    {
        _str = (char*)realloc(_str, ++_len + 1);
        _capacity = _len;
    }
    //strncat(&(_str[pos+1]), &(_str[pos]));
    int dest = _len;
    while (dest > pos)
    {
        _str[dest] = _str[dest - 1];
        dest--;
    }
    _str[pos] = ch;
}
void String::Insert(size_t pos, const char* str)
{
    //assert(pos <= _len);
    if (pos > _len)
        return;

    int len = strlen(str);
    _len += len;
    if (_len > _capacity)
    {
        _str = (char*)realloc(_str, _len + 1);
        _capacity = _len;
    }

    int i = 0;
    while (_len - i > pos + len - 1)
    {
        _str[_len - i] = _str[_len - i - len];
        i++;
    }
    int j = 1;
    while (_len - i - j + 2 >= pos + 1)
    {
        _str[_len - i - j + 1] = str[len - j];
        j++;
    }
}
void String::Erase(size_t pos, size_t count)
{
    //assert(pos < _len);
    if (pos >= _len)
        return;

    if (pos + count >= _len)
    {
        _str[pos] = '\0';
        _len = pos;
    }
    else
    {
        while (pos + count <= _len)
        {
            _str[pos] = _str[pos + count];
            pos++;
        }
        _len -= count;
    }
}
int String::Find(char ch) const
{
    //return strchr(_str, ch);
    int ret = 0;
    while (_str[ret] != ch&&_str[ret] != '\0')
        ret++;
    return _str[ret] == '\0' ? -1 : ret;
}
int String::Find(const char* str) const
{
    //return strstr(_str, str);
    if (*str == '\0')
        return _len;

    int ret = 0;
    size_t len = strlen(str);
    while (ret + len <= _len)
    {
        int i = 0;
        while (_str[ret + i] == str[i] && str[i] != '\0')
            i++;
        if (str[i] == '\0')
            return ret;
        ret++;
    }
    return -1;
}
char& String::operator[](size_t pos)
{
    return _str[pos];
}
bool String::operator==(const String& s) const
{
    if (_len == s._len)
    {
        int i = 0;
        while (_str[i] == s._str[i])
        {
            if (_str[i] == '\0')
                return true;
            i++;
        }
    }
    return false;
}
bool String::operator<(const String& s) const
{
    int i = 0;
    while (_str[i] == s._str[i])
    {
        if (_str[i] == '\0')
            return false;
        i++;
    }
    return _str[i] - s._str[i] < 0 ? true : false;
}
bool String::operator<=(const String& s) const
{
    int i = 0;
    while (_str[i] == s._str[i])
    {
        if (_str[i] == '\0')
            return true;
        i++;
    }
    return _str[i] - s._str[i] < 0 ? true : false;
}
bool String::operator>(const String& s) const
{
    int i = 0;
    while (_str[i] == s._str[i])
    {
        if (_str[i] == '\0')
            return false;
        i++;
    }
    return _str[i] - s._str[i] > 0 ? true : false;
}
bool String::operator>=(const String& s) const
{
    int i = 0;
    while (_str[i] == s._str[i])
    {
        if (_str[i] == '\0')
            return true;
        i++;
    }
    return _str[i] - s._str[i] > 0 ? true : false;
}
bool String::operator!=(const String& s)const
{
    int i = 0;
    while (_str[i] == s._str[i])
    {
        if (_str[i] == '\0')
            return false;
        i++;
    }
    return true;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值