实现简单的string类

摘要

实现了一个string类,包括基本的构造、赋值、判断、大小写等。

String API

Constructors

string();
string(const char& ch);
string(const char* str);
string(const string& str);

Informational Methods

unsigned Length() const;
int Index(char c) const;

Case Methods

void UpCase(unsigned first, unsigned last);
void LowCase(unsigned first, unsigned last);

Stream Operators

friend std::ostream& operator<<(std::ostream& os, const string& str);
friend std::istream& operator>>(std::istream& is, string& str);

Accessor Operators

const char& operator[](unsigned i) const;
char& operator[](unsigned i);

Assignment Operators

String&  operator= (const String&)
String& operator+= (const String&)

String Concatenation

friend string operator+(const string& lhs, const string& rhs);
friend string operator+(const string& lhs, const char* rhs);
friend string operator+(const char* lhs, const string& rhs);

Logical Operators

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);
#include "string.h"

using namespace vlyf;

inline
vlyf::string::string()
{
    length = 0;
    data = new char[0];
}

inline
string::string(const char* str)
{
    if (str)
    {
        unsigned n = 0;
        while (str[n] != '\0') n++;
        data = new char[n];
        length = n;
        for (unsigned i = 0; i < n; i++)
        {
            data[i] = str[i];
        }
    }
    else
    {
        length = 0;
        data = new char[0];
    }
}

inline
string::string(const string& str)
{
    unsigned len = str.Length();
    length = len;
    data = new char[len];
    for (unsigned i = 0; i < len; i++)
    {
        data[i] = str[i];
    }
}

inline
string::~string()
{
    delete[]data;
}

inline
unsigned string::Length() const
{
    return length;
}

int string::Index(char c) const
{
    for (unsigned i = 0; i < Length(); i++)
    {
        if (c == data[i]) return i;
    }
    return -1;
}

inline
void string::UpCase(unsigned first, unsigned last)
{
    while (first++ < last)
    {
        if ('a' <= data[first] && data[first] <= 'z')
            data[first] -= ('a' - 'A');
    }
}

inline
void string::LowCase(unsigned first, unsigned last)
{
    while (first++ < last)
    {
        if ('A' <= data[first] && data[first] <= 'Z')
            data[first] += ('a' - 'A');
    }
}

const char& vlyf::string::operator[](unsigned i) const
{
    return data[i];
}

char& vlyf::string::operator[](unsigned i)
{
    return data[i];
}

string& vlyf::string::operator=(const char* s)
{
    char* temp = data;                  //保存原本数据,确保异常安全性
    unsigned n = 0;
    while (s[n] != '\0')
        n++;
    length = n;
    data = new char[n];
    for (unsigned i = 0; i < n; i++)
        data[i] = s[i];
    delete[] temp;
    return *this;
}

inline
string& vlyf::string::operator=(const string& str)
{
    if (this == &str) return *this;
    char* temp = str.data;              //保存原本数据,确保异常安全性

    unsigned len = str.Length();
    data = new char[len];
    for (unsigned i = 0; i < len; i++)
        data[i] = str[i];
    length = len;
    delete[] temp;
    return *this;
}

inline
string& string::operator+=(const string& str)
{
    unsigned len = length + str.Length();
    char* ss = new char[len];
    for (unsigned i = 0; i < Length(); i++)
        ss[i] = data[i];
    for (unsigned i = 0; i < str.Length(); i++)
        ss[i + length] = str[i];
    delete[]data;
    data = ss;
    length = len;
    return *this;
}

std::ostream& vlyf::operator<<(std::ostream& os, const string& str)
{
    if (str.Length() > 0)
    {
        for (unsigned i = 0; i < str.Length(); i++)
            os << str[i];
    }
    else
    {
        os << "";
    }
    return os;
}

std::istream& vlyf::operator>>(std::istream& is,string& str)
{
    char* ss = new char[1000];
    is >> ss;
    str = string(ss);
    return is;
}

string vlyf::operator+(const string& lhs, const string& rhs)
{
    return string(lhs) += rhs;
}

string vlyf::operator+(const string& lhs, const char* rhs)
{
    return string(lhs) += string(rhs);
}

string vlyf::operator+(const char* lhs, const string& rhs)
{
    return string(lhs) += string(rhs);
}

bool vlyf::operator==(const string& lhs, const string& rhs)
{
    if (lhs.Length() != rhs.Length())
        return false;
    unsigned n = 0;
    unsigned len = lhs.Length();
    while (lhs[n] == rhs[n] && len != n)
        n++;
    return n == len;
}

bool vlyf::operator!=(const string& lhs, const string& rhs)
{
    if (lhs.Length() != rhs.Length())
        return true;
    unsigned n = 0;
    unsigned len = lhs.Length();
    while (lhs[n] == rhs[n] && len != n)
        n++;
    return n != len;
}

bool vlyf::operator<(const string& lhs, const string& rhs)
{
    unsigned min = (lhs.Length() < rhs.Length()) ? lhs.Length() : rhs.Length();
    unsigned n = 0;
    while (lhs[n] == rhs[n] && n != min)
        n++;
    if (n == min) return lhs.Length() < rhs.Length();
    else
    {
        if (lhs[n] < rhs[n])
            return lhs.Length() < rhs.Length();
        else
            return lhs.Length() > rhs.Length();
    }
}

bool vlyf::operator>(const string& lhs, const string& rhs)
{
    return lhs < rhs;
}

int main()
{
    string s1 = "123";
    std::cout << "s1:" << s1 << "       length: " << s1.Length() << std::endl;

    string s2("456");
    std::cout << "s2:" << s2 << "       length: " << s2.Length() << std::endl;

    string s3(s1);
    std::cout << "s3:" << s3 << "       length: " << s3.Length() << std::endl;

    string s4 = s3;
    std::cout << "s4:" << s4 << "       length: " << s4.Length() << std::endl;

    string s5(s1);
    s5 += s2;
    std::cout << "s5:" << s5 << "       length: " << s5.Length() << std::endl;

    string s6(s5);
    std::cout << "s6:" << s6 << "       length: " << s6.Length() << std::endl;

    if (s5 == s6) std::cout << "s5 == s6:       true" << std::endl;

    string s7 = s1 + "456";
    std::cout << "s7:" << s7 << "       length: " << s7.Length() << std::endl;

    string s8 = "456" + s1;
    std::cout << "s8:" << s8 << "       length: " << s8.Length() << std::endl;

    string s9 = s7 + s8;
    std::cout << "s9:" << s9 << "       length: " << s9.Length() << std::endl;

    return 0;
}

原文地址:https://www.cnblogs.com/vlyf/p/11929279.html

时间: 2024-09-28 11:31:33

实现简单的string类的相关文章

简单实现string类

简单实现string类,正确管理资源. 1.首先定义它的成员变量: private:     char* _data;//定义一个字符指针 2.实现它的构造函数: String(const char* str="") //如果定义对象时没有传参数,使用缺省构造参数构造出""字符串     :_data(new char[strlen(str)+1])    //+1表示多创建一个空间存'\0' {     strcpy(_data,str); } 3.实现拷贝构造

简单的String类实现及写时拷贝

#include<iostream> using namespace std; class String { public: /*String(const char* str=" ") :_str(new char[strlen(str)+1]) { strcpy(_str, str); } */ String(const char* str = " ") { if (str == NULL) { _str = new char; _str[0] = '

实现最简单的string类的深拷贝

#include<iostream> using namespace std; class String { public:                 String()                                 :_str( new char [1])                 {                                 _str = '\0';                 }                 String( con

【c++】简单的string类的几个基本函数

// string的几个基本函数的实现 #include <iostream> #include <assert.h> #include <string.h> using namespace std; class String { public: String() { _str = new char[1]; _str[0] = '\0'; } String(char *str) { assert(str != NULL); _str = new char[strlen(

C++ 模拟String类 相关

#define _CRT_SECURE_NO_WARNINGS 1 #include <iostream> #include <cassert> using namespace std; //string 编写 /*版本1 以前的版本*/ /*************** class String { public: //错误 String(char *str = NULL) String(char *str = "") :_str(new char[strle

c++string类的相关操作

string的应用是非常典型的,下面我写了一些简单的string类的相关的一些操作,包括增删查改等一些功能和一些运算符的重载,包括他们的测试用例: #include<iostream>#include<string>#include<assert.h>using namespace std; #define CARA 3;class String{public: String(char *str = "")  :_str(new char[strle

【Java】整理关于java的String类,equals函数和比较操作符的区别

初学 Java 有段时间了,感觉似乎开始入了门,有了点儿感觉但是发现很多困惑和疑问而且均来自于最基础的知识折腾了一阵子又查了查书,终于对 String 这个特殊的对象有了点感悟大家先来看看一段奇怪的程序: public class TestString {       public static void main(String[] args) {           String s1 = "Monday";           String s2 = "Monday&qu

全面深入介绍C++字符串:string类

http://blog.csdn.net/liuliming3000/article/details/1809385 1 从C到C++ string类 2 string类的构造函数 3 string类的字符操作 4 string的特性描述 5 string类的输入输出操作 6 string的赋值 7 string的连接 8 string的比较 9 string类的查找函数 10 string类的替换函数 11 string类的插入函数 12 string类的删除函数 13 string类的迭代器

C++中简单的sting类的实现

String 在C++的学习生涯我中发现String类的功能十分强大,所以我们是很有必要模拟实现它的,况且在面试的时候模拟实现一个String类也是面试官经常会考的,但是因为外界因素的限制我们是不可能模拟的和库里的string一致的(C++库里的string功能更强大),所以今天我们只模拟实现string的基本功能-构造函数,拷贝构造函数,析构函数,赋值运算符重载,运算符+=的重载,运算符[]的重载,c_str(得到一个C风格的字符指针,可操作字符串),Size,Push_Back,Insert