自己模拟写C++中的String类型
下面是模拟实现字符串的相关功能,它包括一下功能:
String(const char * s);//利用字符串来初始化对象
String(); //默认构造函数
String(const String & s);//复制构造函数,利用String类型来初始化对象
~String(); //析构函数
int length(); //返回String类型中字符串的长度
String & operator=(const String & s);//重载=运算符。
String & operator=(const char *);
char & operator[](int i); //重载【】运算符
const char & operator[](int i) const;
friend bool operator<(const String & st,const String & st2);//重载<运算符,用来比较String类型中字符串的大小。
friend bool operator>(const String & st,const String & st2);
friend bool operator==(const String & st,const String & st2);//重载==运算符,判断两个String对象是否相等
friend ostream & operator<<(ostream & os,const String & st2);//重载输出函数
friend istream & operator>>(istream & is,String & st2);//重载输入函数
static int HowMang()//返回总共生成的String类对象的数目。
String.h:
#ifndef STRING_H_INCLUDED
#define STRING_H_INCLUDED
#include"iostream"
#include<string.h>
using std::ostream;
using std::istream;
class String{
private:
char * str;
int len;
public:
static int num_strings;
static const int CINLM=;
String(const char * s);
String();
String(const String & s);
~String();
int length();
String & operator=(const String & s);
String & operator=(const char *);
char & operator[](int i);
const char & operator[](int i) const;
friend bool operator<(const String & st,const String & st2);
friend bool operator>(const String & st,const String & st2);
friend bool operator==(const String & st,const String & st2);
friend ostream & operator<<(ostream & os,const String & st2);
friend istream & operator>>(istream & is,String & st2);
static int HowMang()
{
return num_strings; }
}; #endif // STRING_H_INCLUDED
String.cpp:
#include<iostream>
#include"String.h"
#include"string.h"
using namespace std;
int String::num_strings=;
int String::length()
{
return this->len;
}
String::String(const char * s)
{
len=strlen(s);
str=new char[len+];
num_strings++;
}
String::String()
{
str=;
len=;
num_strings++;
} String::String(const String & s)
{
num_strings++;
len=strlen(s.str);
str=new char[len+];
strcpy(str,s.str);
}
String::~String()
{
--num_strings;
delete [] str;
len=;
}
String & String::operator=(const String & s)
{
if(this==&s)
return *this;
delete [] str;
len=strlen(s.str);
str=new char[len+];
strcpy(str,s.str);
// num_strings++;
}
String & String::operator=(const char * s)
{
len=strlen(s);
str=new char[len+];
strcpy(str,s);
// num_strings++;
}
char & String::operator[](int i)
{
return str[i];
}
const char & String::operator[](int i) const
{
return str[i];
}
bool operator<(const String & st,const String & st2)
{
if(strcmp(st.str,st2.str)<)
return true;
else
return false;
}
bool operator>(const String & st,const String & st2)
{
return (st<st2)==false;
}
bool operator==(const String & st,const String & st2)
{
if(strcmp(st.str,st2.str)>)
return true;
else
return false;
}
ostream & operator<<(ostream & os,const String & st2)
{
os<<st2.str;
return os;
}
istream & operator>>(istream & is,String & st2)
{
char temp[String::CINLM];
is.get(temp,String::CINLM);
if(is)
st2=temp;
while(is&&is.get()!='\n')
continue;
return is;
}
main.cpp:
#include <iostream>
#include"String.h"
using namespace std;
int main()
{
String name[];
char temp[];
int i;
for(i=;i<;i++)
{
cin.get(temp,);
while(cin&&cin.get()!='\n')
continue;
if(!cin&&temp[]=='\0')//如果是空串的话,cin会为false
break;
else
name[i]=temp;
}
int total=i;
int firs=,shor=;
if(total<)
{
cout<<"没有输入"<<endl;
}else{
for(i=;i<total;i++)
{
cout<<name[i][]<<":"<<name[i]<<endl;
}
for(i=;i<total;i++)
{
if(name[i]<name[firs])
firs=i;
if(name[i].length()<name[shor].length())
shor=name[i].length();
}
}
cout<<"最短的字符串为:"<<name[shor]<<endl;
cout<<"最前面的字符串为:"<<name[firs]<<endl;
return ;
}
自己模拟写C++中的String类型的更多相关文章
- C++中关于string类型究竟能不能用cout输出的问题
先让我讲下故事哈 一次在MFC中用cout输出一个string类型字符串,编译时出现这样一个错误: error C2679: binary '<<' : no operator defin ...
- mysql语句中把string类型字段转datetime类型
mysql语句中把string类型字段转datetime类型 在mysql里面利用str_to_date()把字符串转换为日期 此处以表h_hotelcontext的Start_time和En ...
- c/c++中关于String类型的思考
首先说明:String并不是一种内置类型,因此任何通过String声明出来的实例都不是一个变量,不同于内置类型因此String仅仅能称之为一种特殊的型别,没错String是一个类类型. 一般来说c语言 ...
- java中关于String 类型数据 的存储方式
Constant Pool常量池的概念: 在讲到String的一些特殊情况时,总会提到String Pool或者Constant Pool,但是我想很多人都不太 明白Constant Pool到底是个 ...
- 关于switch语句中使用String类型的实现原理
在Java 7 以后,switch语句可以用作String类型上. 从本质来讲,switch对字符串的支持,其实也是int类型值的匹配.它的实现原理如下: 通过对case后面的String对象调用ha ...
- 3、Redis中对String类型的操作命令
写在前面的话:读书破万卷,编码如有神 -------------------------------------------------------------------- ------------ ...
- Java中关于String类型的一些思考
作为初学者在学习Java的时候,变量类型是不可避免会遇到的,在以往我们的印象中字符串String都是作为基本类型而存在的,但是在Java中String类型确是一个实实在在的引用类型,是可以通过new关 ...
- Javascript中的string类型使用UTF-16编码
2019独角兽企业重金招聘Python工程师标准>>> 在JavaScript中,所有的string类型(或者被称为DOMString)都是使用UTF-16编码的. MDN DOMS ...
- Redis中一个String类型引发的惨案
曾经看到这么一个案例,有一个团队需要开发一个图片存储系统,要求这个系统能快速记录图片ID和图片存储对象ID,同时还需要能够根据图片的ID快速找到图片存储对象ID.我们假设用10位数来表示 ...
随机推荐
- python的Web框架:Django路由系统以及模板导入
Django的路由系统 当一个请求来到时 当一个请求来到时 1.首先到项目目录下的urls.py(根URLconf模块)中,查找路由规则: 2.根URELcof模块,里面定义了 urlpatterns ...
- Nullable<T>、Nullable、null、?修饰符的区别
这章我们讨论一下Nullable<T>.Nullable.null.?修饰符的区别 原创文章 Nullable<T>的前世今生 讨论它们之前,我们有必要讨论一下Nullable ...
- winform窗体 小程序【三级联动】
三级联动[省,市,区] 类似地区选择,当选的某个省份,后面的下拉框相对变成对应省份的区县 实现省市区联动关键是数据库的表,[每个省内区的AreaCode列是同样的] public Form2() { ...
- Java虚拟机--垃圾收集器和内存分配
垃圾收集器和内存分配 程序计数器.虚拟机栈.本地方法栈这三个区域和线程的生命周期一致,所以方法结束或者线程结束时,内存自然就跟着回收了.Java堆和方法区,只有在程序处于运行期间才能知道会创建哪些对象 ...
- gulp前端自动化环境搭建详解
1.安装 nodejs Grunt和所有grunt插件都是基于nodejs来运行的, https://nodejs.org/ 安装完成之后在终端 node -v 查看安装版本 npm -v 查看np ...
- 分享一个 jsPDF的简单使用以及中文编码问题的解决
后台一个下载文件中内容国际化问题的坑甩到了前端 前端自己匹配,自己处理国际化,生成文件下载 jsPDF-AutoTable 挺靠谱 中文乱码, 还是好人多啊 解决方式如下文章 jsPDF的简单使 ...
- gis cad导出弧段在arcmap下 不准问题
我发现cad 的图形导出到arcmap下会出现各种各样的丢失问题,特别是cad的弧段在arcmap下会弯曲(弧度指向另外一边). 那么应该怎么解决这个问题呢?后来想到FME可以高效的还原cad的图形, ...
- 使用Git上传代码到Github仓库
准备工作: 首先你需要一个github账号,所有还没有的话先去注册吧! https://github.com/ 我们使用git需要先安装git工具,这里给出下载地址,下载后一路直接安装即可: http ...
- Android热修复之 - 收集崩溃信息上传服务器
1.概述 大致的流程就是在用户崩溃的时候,我们获取崩溃信息.应用当前的信息和手机信息,然后把它保存到手机内存卡,再找我就直接找出来看看.后来衍生到上线后某些奇葩机型会有部分问题,所以不得不上传到服务器 ...
- Android Fragment的用法(二)
如果你经常使用平板电脑,应该会发现很多的平板应用现在都采用的是双页模式(程序会在左侧的面板上显示一个包含子项的列表,在右侧的面板上显示内容),因为平板电脑的屏幕足够大,完全可以同时显示下两页的内容,但 ...