C++ string类的实现
//string类的实现
#include <iostream>
#include <string.h>
using namespace std;
class String{
public:
//构造函数
String(const char *str = ""):
m_str(strcpy(new char[strlen(str)+1],str)){}
//析构函数
~String(void){
delete[] m_str;
m_str = NULL;
}
//深拷贝构造
String(const String &that):
m_str(strcpy(new char[strlen(that.m_str)+1],that.m_str)){}
//拷贝赋值
String &operator = (const String &that){
if(&that != this){
/* 小鸟版
delete[] m_str;
m_str = new char[strlen(that.m_str)+1];
strcpy(m_str,that.m_str);
*/
char *str = new char[strlen(that.m_str)+1];
delete[] m_str;
m_str = strcpy(str,that.m_str);
/*
老鸟版
String temp(that);
swap(m_str,that.m_str);
*/
}
return *this;
}
const char *c_str(void)const{
return m_str;
}
private:
char *m_str;
};
int main(){
String s1("hello,world!");
cout << s1.c_str() << endl;
String s2 = s1;//拷贝构造
cout << s2.c_str() << endl;
String s3("hello C++");
s2 = s3;
cout << s2.c_str() << endl;
return 0;
}
C++ string类的实现的更多相关文章
- 标准库String类
下面的程序并没有把String类的所有成员方法实现,只参考教程写了大部分重要的成员函数. [cpp] view plain copy #include<iostream> #include ...
- 自己实现简单的string类
1.前言 最近看了下<C++Primer>,觉得受益匪浅.不过纸上得来终觉浅,觉知此事须躬行.今天看了类类型,书中简单实现了String类,自己以前也学过C++,不过说来惭愧,以前都是用C ...
- 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.判断输入的用户名和密 ...
随机推荐
- 获取当前方法名,行号,类名,所在java文件第几行
public class Demo { public static void main(String[] args) { Demo demo = new Demo(); demo.go(); } pu ...
- 使用Unified Communications Managed API获取Lync在线会议的链接地址
最近在项目上遇到一个问题,需要能够在程序中获取Lync会议的链接地址.Lync是微软出品的一套即时通信(IM)客户端软件,配合Microsoft Lync Server使用,其前身是Microsoft ...
- 用c#开发的一款webservice调用小工具,方便测试
本人经常与webservice打交道,webservice调用工具有soap ui,使用起来不是那么方便,所以我花了很长时间特意写了个小工具来调用Webservice方便测试,还有待进一步完善.使用方 ...
- 5G
前世 1G 2G 3G 4G 今生 5G 推荐书籍: Gold Smith -<无线通信> David Tse -<无线通信基础> 参考:
- 各种类型转换为字符串类型(ToString())
更详细请参考:http://blog.csdn.net/wanzhuan2010/article/details/8478904 // C 货币 2.5.ToString("C") ...
- 关于C#操作防火墙,阻止程序联网
//开启服务.开启防火墙 public void OpenFileWall() { // 1. 判断当前系统为XP或Win7 RegistryKey rk = Registry.LocalMachin ...
- Xdebug文档(五) 代码覆盖分析
代码覆盖分析能在请求时让你知道脚本哪一行(或哪一段)在执行. 相关设置 xdebug.coverage_enable 类型: boolean, 默认值: 1, 始于 Xdebug >= 2.2 ...
- PHP预定义变量
* PHP预定义 * 预定义变量 * $_GET - 接收客户端以请求类型为GET方法发送的数据内容 * $_POST - 接收客户端以请求类型为POST方法发送的数据内容 * $_REQUEST - ...
- hexo博客进阶-相册和独立域名
之前我已经写了一篇文章详细的讲述了如何使用hexo搭建github博客.如果还没有看的可以去看看,hexo搭建博客 其实,根据这篇文章的过程我们就能够搭建一个专属于自己,并且非常美观的博客了.但是如果 ...
- 本周进步要点20161023(含李笑来第二场live笔记要点)
本周主要忙于去武汉参加iDOF2016智能数字油田会议,会上做了题为“油田SOA及云平台的系统思考与实践”的报告,为了准备这篇报告,用到了一些以前学过的知识,具体内容见“参加iDOF2016会议的收获 ...