自定义String
// 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的更多相关文章
- C++自定义String字符串类,支持子串搜索
C++自定义String字符串类 实现了各种基本操作,包括重载+号实现String的拼接 findSubStr函数,也就是寻找目标串在String中的位置,用到了KMP字符串搜索算法. #includ ...
- C++基础 (5) 第五天 重载new delete () 只能操作符 自定义string类
1 昨日回顾 1.static 对整个类共享 可以直接用 类::方法 调用 如果是私有的 可以提供一个静态的访问静态成员的方法 2 自定义的数组类-重载操作符[] 3 重载new和delete 4 重 ...
- 自定义String类,并且实现在STL容器中添加自定义的类型
13.44 编写标准库string类的简化版本,命名String.你的类应该至少有一个默认构造函数和一个接受C风格字符串指针参数的构造函数.使用allocator为你的String类分配所需内存. 1 ...
- 自定义string类
#include <iostream> #include <cstring> using namespace std; class String; class Data{ // ...
- JVM类加载器是否可以加载自定义的String
前言 曾经有一次,面试官问到类加载机制,相信大多数小伙伴都可以答上来双亲委派机制,也都知道JVM出于安全性的考虑,全限定类名相同的String是不能被加载的.但是如果加载了,会出现什么样的结果呢?异常 ...
- C# String.Format大全
C# String.Format大全 ? ? ? 十进制的数字 ? ? string.Format("{0:D3}",23) 023 格式化十进制的数字 string.Format ...
- 使用引用计数和copy-on_write实现String类
本文写于2017-01-18,从老账号迁移到本账号,原文地址:https://www.cnblogs.com/huangweiyang/p/6295420.html 这算是我开始复习的内容吧,关于st ...
- 【Java面试题】53 能不能自己写个类,也叫java.lang.String?
可以,但是即使你写了这个类,也没有用. 这个问题涉及到加载器的委托机制,在类加载器的结构图(在下面)中,BootStrap是顶层父类,ExtClassLoader是BootStrap类的子类,ExtC ...
- 一道前端面试题:定义一个方法将string的每个字符串间加个空格返回,调用的方式'hello world'.spacify();
偶然在群里看到了这道题:定义一个方法将string的每个字符串间加个空格返回,调用的方式'hello world'.spacify(); 这道题主要是对JavaScript对象原型的考察.
随机推荐
- spring security+freemarker获取登陆用户的信息
spring security+freemarker获取登陆用户的信息 目标页面之间获取 ${Session.SPRING_SECURITY_CONTEXT.authentication.princi ...
- Beautiful Paintings CodeForces - 651B (贪心)
大意: 给定序列$a$, 可以任意排序, 求最大下标i的个数, 满足$a_i<a_{i+1}$. 这个贪心挺好的, 答案就为n-所有数字出现次数最大值.
- https请求排错过程
1. 看请求有没有到nginx 此时需要查看nginx的日志.一般每一个项目都会配置一个nginx站点,而一个站点都会又一个nginx配置文件,这个文件位于哪里呢?不出意外应该在:下面,如果找不到的话 ...
- js删除数组中元素的方法
一.清空数组 var ary = [1,2,3,4]; ary.splice(0,ary.length);//清空数组 console.log(ary); // 输出 [],空数组,即被清空了 二.删 ...
- React Native之FlexBox布局
参考原文链接:https://www.cnblogs.com/wujy/p/5841685.html 弹性盒模型(The Flexible Box Module),又叫Flexbox,意为“弹性布局” ...
- bootstrap fileinput组件的使用
组件的下载地址为:https://github.com/kartik-v/bootstrap-fileinput 比较详细的介绍可参看:http://www.jq22.com/jquery-info5 ...
- Activiti手动执行的应用(UserTask)
工作流模拟某公司请假流程情景如下: 1.开发人员请假流程,如果开发人员请假,如果请假天数小于3天,组长批准,人事批准即可请假. 2.如果请假大约三天,需要项目经理或者产品经理批准,并且项目总监 ...
- [luogu P3384] [模板]树链剖分
[luogu P3384] [模板]树链剖分 题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点 ...
- Mysql锁(翻译)
内容主要是对mysql文档的翻译. 1. shared(s) 共享锁2. exclusive(x) 排它锁 innodb的s锁和x锁是行级锁.事务T1获得s锁,事务T2仍然可以获得s锁.事务T1获得x ...
- 利用ML&AI判定未知恶意程序——里面提到ssl恶意加密流检测使用N个payload CNN + 字节分布包长等特征综合判定
利用ML&AI判定未知恶意程序 导语:0x01.前言 在上一篇ML&AI如何在云态势感知产品中落地中介绍了,为什么我们要预测未知恶意程序,传统的安全产品已经无法满足现有的安全态势.那么 ...