#include <iostream>
#include <cstring>
using namespace std; class String; class Data{ // 抽象基类Data
public:
virtual const int compareTo(const String& target) const = ; // virtual比较函数
}; class String: public Data{ // String类。继承自Data
public:
static int num_strings; // 程序周期内创建的String对象个数
static const int CINLIM = ; // 限制字符长度
String(); // 默认构造函数
String(unsigned int capacity); // 以大小为capacity来初始化
String(unsigned int capacity, char ch); // 填充字符为ch,大小为capacity
String(const char* target); // 以一个c风格字符串初始化
String(const String& st); // 拷贝构造函数
~String(){ delete ptr; ptr = nullptr;} // 析构函数
const int findSubStr(const char* substr) const; // 找出子串位置
const int findChar(char target) const; // 找出字符第一次出现位置
const int length() const; // 返回字符串大小
static int howManyExit(); // 返回程序周期内创建的String对象个数
const int compareTo(const String& target) const override; // 与其它String对象比较大小
String& operator=(const String&); // 重载赋值操作符
String& operator=(const char*); // 重载赋值操作符
friend String operator+(const String&,const String&) ; // 重载加号
char& operator[](int i); // 重载[]
const char& operator[](int i) const; // 重载[]
friend bool operator>(const String& st1,const String& st2); // 重载>
friend bool operator<(const String& st1,const String& st2); // 重载<
friend bool operator==(const String& st1,const String& st2); // 重载==
friend ostream& operator<<(ostream& os,const String& st); // 重载<<
friend istream& operator>>(istream& is,String& st); // 重载>>
private:
char* ptr; // 内置char数组指针
unsigned int len; // 当前String长度
}; int String::num_strings = ; // 静态变量初始化 String::String() { ptr = new char[]; len = ; } String::String(unsigned int capacity){
ptr = new char[capacity]; len = capacity;
++ num_strings;
} String::String(unsigned int capacity, char ch){
ptr = new char[capacity]; len = capacity;
for(int i=;i<len;i++)
ptr[i] = ch;
++ num_strings;
} String::String(const char* target){
int tmp = (int)strlen(target);
len = (unsigned)tmp;
ptr = new char[len];
strcpy(ptr,target);
++ num_strings;
} String::String(const String& st){
int tmp = (int)strlen(st.ptr);
len = (unsigned)tmp;
ptr = new char[len];
strcpy(ptr,st.ptr);
++ num_strings;
} const int String::findSubStr(const char* substr) const{
int next[];
next[] = -;
int i=,j=-;
int lenP = (int)strlen(substr);
while(i<lenP){
if(j==-||substr[i]==substr[j]){
++i; ++j;
next[i] = j;
}else
j = next[j];
}
i = ,j = ;
while(i<len&&j<lenP){
if(j==-|ptr[i]==substr[j]){
++i; ++j;
}else
j = next[j];
}
if(j==lenP) return len - j;
return -;
} const int String::findChar(char target) const{
for(int i=;i<len;i++)
if(ptr[i] == target) return i;
return -;
} const int String::length() const{
return this->len;
} int String::howManyExit(){
return num_strings;
} const int String::compareTo(const String& target) const{
if(target.ptr == nullptr || strcmp(ptr,target.ptr)>)
return ;
else if(strcmp(ptr,target.ptr)==)
return ;
else
return -;
} String& String::operator=(const String& str){
if(this == &str)
return *this;
delete ptr;
len = (int)strlen(str.ptr);
ptr = new char[len];
strcpy(ptr,str.ptr);
return *this;
} String& String::operator=(const char* str){
delete[] ptr;
len = (int)strlen(str);
ptr = new char[len+];
strcpy(ptr,str);
return *this;
} String operator+(const String& st1,const String& st2){
int lenSt2 = (int)strlen(st2.ptr);
int lenSt1 = (int)strlen(st1.ptr);
char* temp = new char[lenSt1+lenSt2+];
strcpy(temp,st1.ptr);
for(int i=lenSt1;i<lenSt1+lenSt2;i++)
temp[i] = st2.ptr[i-lenSt1];
return String(temp);
} char& String::operator[](int i){
return ptr[i];
}
const char& String::operator[](int i) const{
return ptr[i];
} bool operator<(const String& st1,const String& st2){
return (std::strcmp(st1.ptr,st2.ptr)<);
}
bool operator>(const String& st1,const String& st2){
return (std::strcmp(st1.ptr,st2.ptr)>);
}
bool operator==(const String& st1,const String& st2){
return (std::strcmp(st1.ptr,st2.ptr)==);
}
ostream& operator<<(ostream& os,const String& st){
os<<st.ptr;
return os;
}
istream& operator>>(istream& is,String& st){
char temp[String::CINLIM];
is.get(temp,String::CINLIM);
if(is)
st = temp; //在此处" = "已被重载过了
while(is && is.get()!='\n')
continue;
return is;
}
int main()
{
String A = "abcd";
String B = "efgh";
String C = A + B;
cout << C << endl;
}

转自:https://www.cnblogs.com/1Kasshole/archive/2018/07/28/9382828.html

自定义string类的更多相关文章

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

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

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

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

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

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

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

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

  5. java中 引用传递、值传递的理解(数组,自定义类,基本数据类型,String类)

    代码部分: public static void main(String[] args) { testInt(); testString(); testArray(); testX(); } publ ...

  6. java自定义注解类

    一.前言 今天阅读帆哥代码的时候,看到了之前没有见过的新东西, 比如java自定义注解类,如何获取注解,如何反射内部类,this$0是什么意思? 于是乎,学习并整理了一下. 二.代码示例 import ...

  7. 关于MapReduce中自定义分区类(四)

    MapTask类 在MapTask类中找到run函数 if(useNewApi){       runNewMapper(job, splitMetaInfo, umbilical, reporter ...

  8. kettle系列-[KettleUtil]kettle插件,类似kettle的自定义java类控件

    该kettle插件功能类似kettle现有的定义java类插件,自定java类插件主要是支持在kettle中直接编写java代码实现自定特殊功能,而本控件主要是将自定义代码转移到jar包,就是说自定义 ...

  9. java 28 - 4 JDK5的新特性 之 枚举的概述和自定义枚举类

    枚举 枚举概述 是指将变量的值一一列出来,变量的值只限于列举出来的值的范围内.举例:一周只有7天,一年只有12个月等. 回想单例设计模式:单例类是一个类只有一个实例 那么多例类就是一个类有多个实例,但 ...

随机推荐

  1. springboot+支付宝完成秒杀项目的初体验

    springboot+支付宝完成秒杀项目的初体验 思考的问题: 首先是秒杀的商品查询,考虑到是热点数据,所以写一个接口读取当日批次的秒杀商品到redis中(那么接下来对商品的操作都放入redis中). ...

  2. machine learning(11) -- classification: advanced optimization 去求cost function最小值的方法

    其它的比gradient descent快, 在某些场合得到广泛应用的求cost function的最小值的方法 when have a large machine learning problem, ...

  3. 如何在vscode中用standard style 风格去验证 vue文件

    1 JavaScript Standard Style简介 本工具通过以下三种方式为你(及你的团队)节省大量时间: 无须配置. 史上最便捷的统一代码风格的方式,轻松拥有. 自动代码格式化. 只需运行 ...

  4. Java出现 The server time zone value '�й���׼ʱ��' is unrecognized 异常

    解决办法: 在配置连接数据库的URL后面加上?serverTimezone=UTC ,如下: jdbc:mysql://localhost:3306/test?serverTimezone=UTC

  5. 关于前端 jQuery 面试的知识点

    参考一个博主整理的一些前端 jQuery 的一些面试题 参考博客:https://www.cnblogs.com/dashucoding/p/11140325.html 参考博客:https://ww ...

  6. 利用Intellij+MAVEN搭建Spring+Mybatis+MySql+SpringMVC项目详解

    http://blog.csdn.net/noaman_wgs/article/details/53893948 利用Intellij+MAVEN搭建Spring+Mybatis+MySql+Spri ...

  7. LeetCode 320. Generalized Abbreviation

    原题链接在这里:https://leetcode.com/problems/generalized-abbreviation/ 题目: Write a function to generate the ...

  8. Mongodb 分片 手动维护chunk

    去年的笔记 For instance, if a chunk represents a single shard key value, then MongoDB cannot split the ch ...

  9. https web service in Tibco & PC

    Error: 1.Certificate signature validation failed , Signature does not matchuse wrong public certific ...

  10. 利用亚马逊AWS搭建个人服务器

    转载博客地址:https://www.jianshu.com/p/a045d4217175