美文网首页
C++ 简易String类

C++ 简易String类

作者: Haoqian | 来源:发表于2015-10-07 22:09 被阅读61次

以下是C++实现的简易String类,需要注意和小心函数声明里面的参数类型(特别是引用和const关键字的使用!)

#include <iostream>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
using namespace std;

class Str{
private:
    char *str;
    char length;
public:
    Str():str(NULL), length(0){}
    Str(char *s, int l){
        str = (char*)malloc((l+1)*sizeof(char));
        strcpy(str, s);
        length = l;
    }
    int size(){ return length;  }
    Str operator+(const Str &s){
        char *ss = (char*)malloc((s.length + length+1)*sizeof(char));
        //length = s.length + length;
        strcpy(ss, str);
        strcat(ss, s.str);
        //free(str);
        return Str(ss, s.length + length);
    }

    Str(const Str &s){
        length = s.length;
        str = (char*)malloc((length + 1)*sizeof(char));
        strcpy(str, s.str);
    }

    Str& operator+=(const Str &s){
        char *ss = (char*)malloc((s.length + length + 1)*sizeof(char));
        length = s.length + length;
        strcpy(ss, str);
        free(str);
        strcat(ss, s.str);
        str = ss;
        return *this;
    }

    Str& operator=(const Str &s){
        if (this == &s) return *this;
        length = s.length;
        char *ss = (char*)malloc((length + 1)*sizeof(char));
        free(str);
        strcpy(ss, s.str);
        str = ss;
        return *this;
    }

    bool operator==(const Str &s){
        if (s.length != length) return false;
        else return strcmp(s.str, str) == 0;
    }

    friend ostream &operator<<(ostream &out, Str &ss){
        out << ss.str;
        return out;
    }
    ~Str()
    { 
        free(str); 
    }
};


int main()
{
    char s[100], s2[100];
    strcpy(s, "this is my string");
    strcpy(s2, "this is my house");
    Str str(s, strlen(s));
    Str str2(s2, strlen(s2));
    cout << str << endl;
    cout << str2 << endl;
    cout << str + str2 << endl;
    cout << str << endl;
    cout << str2 << endl;
    str += str2;
    cout << str << endl;
    cout << str2 << endl;
    str = str2;
    cout << str << endl;
    cout << str2 << endl;
    Str str4 = str2;
    cout << str2 << endl;
    cout << str4 << endl;
    return 0;
}

相关文章

  • C++ 简易String类

    以下是C++实现的简易String类,需要注意和小心函数声明里面的参数类型(特别是引用和const关键字的使用!)

  • c++中的字符串string和C语言中的字符char

    c++中的字符串string 在c++中使用string类,必须在程序中包含头文件string #include ...

  • 高质量C++/C编程指南(转)

    1 有如下的c++类 class String { public: String(const char *str ...

  • 字符串

    C++提供了两种字符串的表示形式: C风格字符 C++引入的string类型 C风格 函数 C++中的String类

  • string的用法

    1、使用string头文件 要想使用标准C++中string类,必须要包含: 或者 string和wstring的...

  • C/C++语言基础

    语言基础 字符串类-string 常量与变量 运算符 程序和语句 字符串类-string (属于类类型)(c++中...

  • C++ string类

    构造函数 注意最后一个构造函数,用n个c来初始化字符串。例如string(5, 'a')就表示字符串"aaaaa"...

  • C++ String 类

    源码 运行结果

  • C++STL整理

    C++ STL中最基本以及最常用的类或容器string、vector、set、list、map string 处理...

  • 十四、自定义String类

    用Sublime写的C++自定义String类,重点在重构!

网友评论

      本文标题:C++ 简易String类

      本文链接:https://www.haomeiwen.com/subject/mlkncttx.html