自己实现 String 类
实现Stirng类:普通构造、复制构造、赋值函数、重载输出函数 <<(友元)
#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std; class String
{
public:
String (const char *str=NULL); //普通构造函数
String (const String &other); //复制构造函数
~String (void); //析构函数
String &operator=(const String &other); //赋值函数
friend ostream &operator<<(ostream &output, const String &other); //输出函数
private:
char *data; //保存字符串
}; //普通构造函数
String::String(const char *str)
{
if (str == NULL)
{
data = new char[];
*data = '\0';
}
else
{
int len = strlen(str);
data = new char[len+];
strcpy(data, str);
}
} //复制构造函数
String::String(const String &other)
{
int len = strlen(other.data);
data = new char[len+];
strcpy(data, other.data);
} //析构函数
String::~String()
{
delete[] data;
} //赋值函数
String &String::operator=(const String &other)
{
if (this == &other)
{
return *this;
}
delete[] data; int len = strlen(other.data);
data = new char[len+];
strcpy(data, other.data); return *this;
} //输出函数
ostream &operator<<(ostream &output, const String &other)
{
output<<other.data;
return output;
} int main()
{
String s1("hello");
cout << s1 << endl;
String s2=s1;
cout << s2 << endl;
String s3;
s3=s1;
cout << s3 << endl;
}
自己实现 String 类的更多相关文章
- 标准库String类
下面的程序并没有把String类的所有成员方法实现,只参考教程写了大部分重要的成员函数. [cpp] view plain copy #include<iostream> #include ...
- 自己实现简单的string类
1.前言 最近看了下<C++Primer>,觉得受益匪浅.不过纸上得来终觉浅,觉知此事须躬行.今天看了类类型,书中简单实现了String类,自己以前也学过C++,不过说来惭愧,以前都是用C ...
- C++ string类的实现
c++中string类的实现 今天面试被考到了, 全给忘记了!!! //string类的实现 #include <iostream> #include <string.h> ...
- String类的功能
String类 标红的为较少出现的 1.判断功能 boolean equals(Object obj) :比较字符串内容是否相同,区分大小写 boolean equalsIg ...
- java基础复习:final,static,以及String类
2.final 1)为啥String是final修饰的呢? 自己答: 答案: 主要是为了“效率” 和 “安全性” 的缘故.若 String允许被继承, 由于它的高度被使用率, 可能会降低程序的性能,所 ...
- String类和StringBuffer类的区别
首先,String和StringBuffer主要有2个区别: (1)String类对象为不可变对象,一旦你修改了String对象的值,隐性重新创建了一个新的对象,释放原String对象,StringB ...
- 05_整理String类的Length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArray()使用说明
Question: 整理String类的Length().charAt(). getChars().replace(). toUpperCase(). toLowerCase().trim().toC ...
- 标准C++中的string类的用法总结
标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...
- String类常用方法
1.String类的特点,字符串一旦被初始化就不会被改变. 2.String对象定义的两种方式 ①String s = "affdf";这种定义方式是在字符串常量池中创建一个Str ...
- 运用String类实现一个模拟用户登录程序
package Test; import java.util.Scanner; // 模拟用户登录程序 // 思路: // 1.用两个String类分别接收用户名和密码 // 2.判断输入的用户名和密 ...
随机推荐
- uva11078 - Open Credit System(动态维护关键值)
这道题使用暴力解法O(n*n)会超时,那么用动态维护最大值可以优化到O(n).这种思想非常实用. #include<iostream> #include<cstdio> #in ...
- java微信学习 接入
现在实习的公司要做微信开发,然而一直没安排任务,所以一直在看微信接口,记录下学习的内容 微信开发肯定要看的就是微信公众平台开发者文档,上面有每种接口的调用格式,刚开始学习的时候自己申请了一个订阅号,个 ...
- python2.7 爬取简书30日热门专题文章之简单分析_20170207
昨天在简书上写了用Scrapy抓取简书30日热门文章,对scrapy是刚接触,跨页面抓取以及在pipelines里调用settings,连接mysql等还不是很熟悉,今天依旧以单独的py文件区去抓取数 ...
- BZOJ4355: Play with sequence
BZOJ4355: Play with sequence https://lydsy.com/JudgeOnline/problem.php?id=4355 分析: 模板题. 把\(2\)操作看成先区 ...
- notepad++怎么显示项目的目录树?
转:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1013/1762.html
- Inception-Resnet-V2
零.Inception-Resnet-V2的网络模型 整体结构如下,整体设计简洁直观: 其中的stem部分网络结构如下,inception设计,并且conv也使用了7*1+1*7这种优化形式: inc ...
- Linux 优秀软件
本文由Suzzz原创,发布于 http://www.cnblogs.com/Suzzz/p/4038925.html ,转载请保留此声明 一些Linux下的优秀软件,个人非常喜欢.都在Ubuntu14 ...
- [转] CentOS系统目录学习
最近初学Linux 对linux的目录产生了很多疑问,看到这篇文章,让我顿时对目录有了一个清晰的认识!推荐给大家! ------------------------------------------ ...
- 新建 FrameMaker API 时引用目录的设置
如果将FDK安装目录下的Sample项目拷贝到其它目录编译,往往会报错 c1083 找不到fapi.h等头文件,或者Link时报错找不到.lib文件. 1.可通过菜单-项目-xxx属性-配置属性-c/ ...
- 服务器启动时Webapp的web.xml中配置的加载顺序
一 1.启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点. 2.紧急着,容创建一个Ser ...