下面是模拟实现字符串的相关功能,它包括一下功能:
    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类型的更多相关文章

  1. C++中关于string类型究竟能不能用cout输出的问题

    先让我讲下故事哈 一次在MFC中用cout输出一个string类型字符串,编译时出现这样一个错误: error C2679: binary '<<' : no operator defin ...

  2. mysql语句中把string类型字段转datetime类型

    mysql语句中把string类型字段转datetime类型   在mysql里面利用str_to_date()把字符串转换为日期   此处以表h_hotelcontext的Start_time和En ...

  3. c/c++中关于String类型的思考

    首先说明:String并不是一种内置类型,因此任何通过String声明出来的实例都不是一个变量,不同于内置类型因此String仅仅能称之为一种特殊的型别,没错String是一个类类型. 一般来说c语言 ...

  4. java中关于String 类型数据 的存储方式

    Constant Pool常量池的概念: 在讲到String的一些特殊情况时,总会提到String Pool或者Constant Pool,但是我想很多人都不太 明白Constant Pool到底是个 ...

  5. 关于switch语句中使用String类型的实现原理

    在Java 7 以后,switch语句可以用作String类型上. 从本质来讲,switch对字符串的支持,其实也是int类型值的匹配.它的实现原理如下: 通过对case后面的String对象调用ha ...

  6. 3、Redis中对String类型的操作命令

    写在前面的话:读书破万卷,编码如有神 -------------------------------------------------------------------- ------------ ...

  7. Java中关于String类型的一些思考

    作为初学者在学习Java的时候,变量类型是不可避免会遇到的,在以往我们的印象中字符串String都是作为基本类型而存在的,但是在Java中String类型确是一个实实在在的引用类型,是可以通过new关 ...

  8. Javascript中的string类型使用UTF-16编码

    2019独角兽企业重金招聘Python工程师标准>>> 在JavaScript中,所有的string类型(或者被称为DOMString)都是使用UTF-16编码的. MDN DOMS ...

  9. Redis中一个String类型引发的惨案

    ​      曾经看到这么一个案例,有一个团队需要开发一个图片存储系统,要求这个系统能快速记录图片ID和图片存储对象ID,同时还需要能够根据图片的ID快速找到图片存储对象ID.我们假设用10位数来表示 ...

随机推荐

  1. java中数据的传递方式到底是怎样的!

    今天早上我了一道有关java的题.主要考点是考java中值得传递方式. 之前我在javaoo里总结的是:基本数据类型中保存的是实际的值,引用数据类型保存的是被引用的内存地址,那么基本数据类型就是按值传 ...

  2. mongodb操作技巧

    1.添加字段或更新值 db.getCollection('test').updateMany( {}, { $set:{ 'createTime':'2017-06-29 08:08', 'updat ...

  3. Spring Boot(三):Spring Boot 中 Redis 的使用

    Spring Boot 对常用的数据库支持外,对 Nosql 数据库也进行了封装自动化. Redis 介绍 Redis 是目前业界使用最广泛的内存数据存储.相比 Memcached,Redis 支持更 ...

  4. SQL Server “复制”表结构,创建_Log表及触发器

    实例效果: 实现表数据的增修删时,记录日志. 1.“复制”现有表, 创建相应的_Log表: (注意点: 通过select union all 的方式,避免了IDENTITY 的“复制”,即如果原表有 ...

  5. 关于winform文本框怎么实现html的placeholder效果

    winfrom默认是不支持这种操作的,此时需要重写控件操作,具体代码如下: public class TextBoxEx : TextBox { public String PlaceHolderSt ...

  6. MFC控件之Combo Box

    下拉链表Combo-box Control 常用属性: Sort:对添加到列表框的字符串进行自动排序.(对指定位置的元素项无效) Type:有三个类型 Simple:没有下拉按钮,可以输入字符串,可以 ...

  7. ABB机器人---PCSDK简介

    BB机器人为用户提供了大量便捷的二次开发及应用工具,PCSDK就是其中一项. 1) 首先,机器人使用PCSDK,必须要有pc interface选项. 2)此处举例使用C#编写简单界面,实现与机器人数 ...

  8. 【转】.NET NPOI操作Excel常用函数

    最近因项目接触了NPOI,感觉还是蛮不错的,网络上的教程普遍版本较老,本篇记录所常用操作,采用NPOI 2.0版本. 推荐: NPOI官方网站 NPOI 1.2.4/1.2.5 官方教程 新建Exce ...

  9. python与中文的那点事

    目录 python与中文的那点事 1. utf-8/gbk/unicode/ASCII 2.各种编码之间的转换 3. 统计字符串中数字,字母,汉字的个数 python与中文的那点事 在学习python ...

  10. 山东省第四届acm解题报告(部分)

    Rescue The PrincessCrawling in process... Crawling failed   Description Several days ago, a beast ca ...