// ShStringNew.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include<iostream>
#include<iomanip>
using namespace std;
class SHString
{ public:
SHString(const char* Str = "")
:mStr(new char[strlen(Str) + 1])
{
strcpy(mStr, Str);
}
SHString(const SHString& Str)
:mStr(new char[strlen(Str.mStr) + 1])
{
strcpy(mStr, Str.mStr);
}
SHString& operator=(const char* Str)
{
delete[] mStr;
mStr = new char[strlen(Str) + 1];
strcpy(mStr, Str);
return *this;
}
SHString& operator=(const SHString& Str)
{
if (this != &Str)
{
delete[]mStr;
mStr = new char[strlen(Str.mStr) + 1];
strcpy(mStr, Str.mStr);
}
return *this;
}
~SHString()
{
delete[]mStr;
mStr = NULL;
} public:
char* mStr;
friend ostream& operator<<(ostream& os, SHString& S);
friend istream& operator >> (istream&, SHString& S);
SHString operator+(const SHString &other); //operator+ }; //因为模拟实现的string不是内置类型,所以要重载输出运算符,才能输出String类对象的内容
ostream& operator<<(ostream& os, SHString& Str)
{
os << Str.mStr;
return os;
} istream &operator >> (istream &input, SHString &s)
{
char temp[255]; //用于存储输入流
input >> setw(255) >> temp;
s = temp; //使用赋值运算符
return input; //使用return可以支持连续使用>>运算符
} inline SHString SHString::operator+(const SHString &other)
{
SHString NewString; NewString.mStr = new char[strlen(mStr) + strlen(other.mStr) + 1];
strcpy(NewString.mStr, mStr);
strcat(NewString.mStr, other.mStr); return NewString;
}
int main()
{
SHString Str1("abcdef");
SHString Str2("ABCDEF");
SHString Str3(Str1);
SHString Str4,Str5,Str6;
Str4 = Str1;
char* v1 = "123";
Str5 = v1;
Str6 = Str4 + Str5;
cout << "Str1->" << Str1 << endl;
cout << "Str2->" << Str2 << endl;
cout << "Str3->" << Str3 << endl;
cout << "Str4->" << Str4 << endl;
cout << "Str5->" << Str5 << endl;
cout << "Str6->" << Str6 << endl;
return 0;
}

  

自定义String的更多相关文章

  1. C++自定义String字符串类,支持子串搜索

    C++自定义String字符串类 实现了各种基本操作,包括重载+号实现String的拼接 findSubStr函数,也就是寻找目标串在String中的位置,用到了KMP字符串搜索算法. #includ ...

  2. C++基础 (5) 第五天 重载new delete () 只能操作符 自定义string类

    1 昨日回顾 1.static 对整个类共享 可以直接用 类::方法 调用 如果是私有的 可以提供一个静态的访问静态成员的方法 2 自定义的数组类-重载操作符[] 3 重载new和delete 4 重 ...

  3. 自定义String类,并且实现在STL容器中添加自定义的类型

    13.44 编写标准库string类的简化版本,命名String.你的类应该至少有一个默认构造函数和一个接受C风格字符串指针参数的构造函数.使用allocator为你的String类分配所需内存. 1 ...

  4. 自定义string类

    #include <iostream> #include <cstring> using namespace std; class String; class Data{ // ...

  5. JVM类加载器是否可以加载自定义的String

    前言 曾经有一次,面试官问到类加载机制,相信大多数小伙伴都可以答上来双亲委派机制,也都知道JVM出于安全性的考虑,全限定类名相同的String是不能被加载的.但是如果加载了,会出现什么样的结果呢?异常 ...

  6. C# String.Format大全

    C# String.Format大全 ? ? ? 十进制的数字 ? ? string.Format("{0:D3}",23) 023 格式化十进制的数字 string.Format ...

  7. 使用引用计数和copy-on_write实现String类

    本文写于2017-01-18,从老账号迁移到本账号,原文地址:https://www.cnblogs.com/huangweiyang/p/6295420.html 这算是我开始复习的内容吧,关于st ...

  8. 【Java面试题】53 能不能自己写个类,也叫java.lang.String?

    可以,但是即使你写了这个类,也没有用. 这个问题涉及到加载器的委托机制,在类加载器的结构图(在下面)中,BootStrap是顶层父类,ExtClassLoader是BootStrap类的子类,ExtC ...

  9. 一道前端面试题:定义一个方法将string的每个字符串间加个空格返回,调用的方式'hello world'.spacify();

    偶然在群里看到了这道题:定义一个方法将string的每个字符串间加个空格返回,调用的方式'hello world'.spacify(); 这道题主要是对JavaScript对象原型的考察.

随机推荐

  1. CentOS优化

    一.CentOS6.x优化 #.更改yum源 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup ...

  2. 基于bootstrap模态框的日期选择器

    近来由于工作需求,以bootstrap模态框+DIV+CSS+JS做了一个适用于移动端的日期选择器,能够满足多样的需求,目前处于第一个版本,后续可能会继续更新.废话不多说,直接进入制作过程. 首先,需 ...

  3. ssh登陆报错:packet_write_wait: Connection to x.x.x.x port 22: Broken pipe

    ssh登陆报错:packet_write_wait: Connection to x.x.x.x port 22: Broken pipe 参考文章: https://patrickmn.com/as ...

  4. jquery快速获得url 的get传值

    <script> var res = location.search.substr(1).split("&"); var arr={}; for (var i ...

  5. css单位分析、颜色设置与调色板

    CSS单位分析 px:单位代表像素,1px代表一个像素点. %:设置子元素为父容器的占比. em:代表该元素中一个字体所占字符,常用在文字首行缩进.其具有继承性. rem:始终代表html中的字符所在 ...

  6. React Router页面传值的三种方法

    文章地址:https://blog.csdn.net/qq_23158083/article/details/68488831

  7. RabbitMQ、Redis、Memcache

    Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...

  8. Intersecting Lines

    Intersecting Lines We all know that a pair of distinct points on a plane defines a line and that a p ...

  9. 如何解决Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 8000401a. 问题

    在系统中需要把数据导出到excel并且压缩,然后报了这个问题: 我在网站上找到了方法: 地址:https://social.msdn.microsoft.com/Forums/vstudio/en-U ...

  10. wine和cygwin安装使用教程

    可以简单地认为wine和cygwin就是功能相反的两个东西.wine是linux的windows模拟环境,让linux可以运行windows程序:cygwin是windows的linux模拟环境,让w ...