#include<iostream>
using namespace std;
 
class String{
    friend ostream& operator<< (ostream&,String&);
public:
    String(const char* str=NULL);                //赋值构造兼默认构造函数(char)
    String(const String &other);                   //赋值构造函数(String)
    String& operator=(const String&other);         //operator=
    String operator+(const String &other)const;  //operator+
    bool operator==(const String&);              //operator==
    char& operator[](unsigned int);                 //operator[]
    size_t size(){return strlen(m_data);};
    ~String(void) {delete[] m_data;}
private:
    char *m_data;
};
inline String::String(const char* str)
{
    if (!str) m_data=0;
    else
    {
        m_data = new char[strlen(str)+1];
        strcpy(m_data,str);
    }
}
inline String::String(const String& other)
{
    if(!other.m_data) m_data=0;
    else
    {
        m_data=new char[strlen(other.m_data)+1];
        strcpy(m_data,other.m_data);
    }
}
inline String& String::operator=(const String& other)
{
    if (this!=&other)
    {
        delete[] m_data;
        if(!other.m_data) m_data=0;
        else
        {
            m_data = new char[strlen(other.m_data)+1];
            strcpy(m_data,other.m_data);
        }
    }
    return *this;
}
inline String String::operator+(const String &other)const
{
    String newString;
    if(!other.m_data)
        newString = *this;
    else if(!m_data)
        newString = other;
    else
    {
        newString.m_data = new char[strlen(m_data)+strlen(other.m_data)+1];
        strcpy(newString.m_data,m_data);
        strcat(newString.m_data,other.m_data);
    }
    return newString;
}
inline bool String::operator==(const String &s)   
{
    if ( strlen(s.m_data) != strlen(m_data) )
        return false;
    return strcmp(m_data,s.m_data)?false:true;
}
inline char& String::operator[](unsigned int e)
{
    if (e>=0&&e<=strlen(m_data))
        return m_data[e];
}
ostream& operator<<(ostream& os,String& str)
{
    os << str.m_data;
    return os;
}
void main()
{
    String str1="Hello!";
    String str2="Teacher!";
    String str3 = str1+str2;
    cout<<str3<<"\n"<<str3.size()<<endl;
    cin.ignore(100, '\n');
    cin.get();
}

string实现的更多相关文章

  1. 透过WinDBG的视角看String

    摘要 : 最近在博客园里面看到有人在讨论 C# String的一些特性. 大部分情况下是从CODING的角度来讨论String. 本人觉得非常好奇, 在运行时态, String是如何与这些特性联系上的 ...

  2. JavaScript String对象

    本编主要介绍String 字符串对象. 目录 1. 介绍:阐述 String 对象的说明以及定义方式. 2. 实例属性:介绍 String 对象的实例属性: length. 3. 实例方法:介绍 St ...

  3. ElasticSearch 5学习(9)——映射和分析(string类型废弃)

    在ElasticSearch中,存入文档的内容类似于传统数据每个字段一样,都会有一个指定的属性,为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成字符串值,Elasticsearc ...

  4. [C#] string 与 String,大 S 与小 S 之间没有什么不可言说的秘密

    string 与 String,大 S 与小 S 之间没有什么不可言说的秘密 目录 小写 string 与大写 String 声明与初始化 string string 的不可变性 正则 string ...

  5. js报错: Uncaught RangeError: Invalid string length

    在ajax请求后得到的json数据,遍历的时候chrome控制台报这个错误:Uncaught RangeError: Invalid string length,在stackoverflow查找答案时 ...

  6. c# 字符串连接使用“+”和string.format格式化两种方式

    参考文章:http://www.liangshunet.com/ca/201303/218815742.htm 字符串之间的连接常用的两种是:“+”连接.string.format格式化连接.Stri ...

  7. 【手记】注意BinaryWriter写string的小坑——会在string前加上长度前缀length-prefixed

    之前以为BinaryWriter写string会严格按构造时指定的编码(不指定则是无BOM的UTF8)写入string的二进制,如下面的代码: //将字符串"a"写入流,再拿到流的 ...

  8. JavaScript中String对象的方法介绍

    1.字符方法 1.1 charAt() 方法,返回字符串中指定位置的字符. var question = "Do you like JavaScript?"; alert(ques ...

  9. 在多线程编程中lock(string){...}隐藏的机关

    常见误用场景:在订单支付环节中,为了防止用户不小心多次点击支付按钮而导致的订单重复支付问题,我们用 lock(订单号) 来保证对该订单的操作同时只允许一个线程执行. 这样的想法很好,至少比 lock( ...

  10. BCL中String.Join的实现

    在开发中,有时候会遇到需要把一个List对象中的某个字段用一个分隔符拼成一个字符串的情况.比如在SQL语句的in条件中,我们通常需要把List<int>这样的对象转换为“1,2,3”这样的 ...

随机推荐

  1. t60替换alt,super,ctrl

    发现T60的左边在ctrl 与 alt 有个win 键,所以就进行了映射 网上有一个把alt->ctrl, super-> alt, ctrl->super的script, 见 ht ...

  2. Netty Bootstrap(图解)|秒懂

    目录 Netty Bootstrap(图解) 源码工程 写在前面 图解几个重要概念 父子 channel EventLoop 线程与线程组 通道与Reactor线程组 Channel 通道的类型 启动 ...

  3. json (js对象标记)

    基础 JSON: JavaScript Object Notation (JavaScript对象表示法) 网络媒体类型是 application/json,文件名扩展是 .json JSON 独立于 ...

  4. iOS 10 权限配置大全

    <!-- 相册 --> <key>NSPhotoLibraryUsageDescription</key> <string>App需要您的同意,才能访问 ...

  5. 搭建vps***

    快速搭建ShadowSocks wget --no-check-certificate -O shadowsocks.sh https://raw.githubusercontent.com/tedd ...

  6. RaspBerry Pi3 ~ 内核编译

    RaspBerryPi3-内核编译 转载注明出处:http://www.cnblogs.com/einstein-2014731/p/5985128.html 在有道云笔记的同步分享:http://n ...

  7. JVM性能分析工具详解--MAT等

    获得堆转储文件 巧妇难为无米之炊,我们首先需要获得一个堆转储文件.为了方便,本文采用的是 Sun JDK 6.通常来说,只要你设置了如下所示的 JVM 参数: -XX:+HeapDumpOnOutOf ...

  8. spring 'arroudAspect' for bean class [com.dw.test.ArroudAspect] conflicts with existing, non-compatible bean definition of same name and class [com.dw.aspect.ArroudAspect]

    Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: ...

  9. SDUT OJ 河床

    河床 Time Limit: 3000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 地理学家们经常要对一段河流进行测量分析.他们从上游开始向下游方向等距离地选择 ...

  10. 2017SN多校D1T1 loveletter:模拟

    题意: 两张n*m的纸.一张透明纸,上面有些地方是黑的,有些地方是透明的.另一张纸上写满了字母. 你需要将透明纸放在写字的纸上,分别顺时针旋转0°.90°.180°.270°,每次从上到下.从左到右写 ...