string类的具体实现
bigThree,注意拷贝构造函数的写法。
#ifndef __MYSTRING__
#define __MYSTRING__ class String
{
public:
String(const char* cstr=);
String(const String& str);
String& operator=(const String& str);
~String();
char* get_c_str() const { return m_data; }
private:
char* m_data;
}; #include <cstring> inline
String::String(const char* cstr)
{
if (cstr) {
m_data = new char[strlen(cstr)+];
strcpy(m_data, cstr);
}
else {
m_data = new char[];
*m_data = '\0';
}
} inline
String::~String()
{
delete[] m_data;
} inline
String& String::operator=(const String& str)
{
if (this == &str)
return *this; delete[] m_data;
m_data = new char[ strlen(str.m_data) + ];
strcpy(m_data, str.m_data);
return *this;
}
//考虑异常安全性的拷贝赋值运算符
inline
String& String::operator=(cosnt String& str){
if(this != &str){
String strTmp(str);
char* pTmp = str.m_data;
strTmp.m_data = m_data;
m_data = pTmp;
}
return *this;
} inline
String::String(const String& str)
{
m_data = new char[ strlen(str.m_data) + ];
strcpy(m_data, str.m_data);
} #include <iostream>
using namespace std; ostream& operator<<(ostream& os, const String& str)
{
os << str.get_c_str();
return os;
} #endif
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.判断输入的用户名和密 ...
随机推荐
- 链剖-What you are?-大话西游-校内oj2440
This article is made by Jason-Cow.Welcome to reprint.But please post the writer's address. http://ww ...
- Multisim 如何调整编辑界面大小
1.option -> sheet properties 2.选择workspace
- bugku 求getshell
要修改三个地方 根据大佬们的writeup,要修改三个地方: 1.扩展名filename 2.filename下面一行的Content-Type:image/jpeg 3.最最最重要的是请求头里的Co ...
- AcWing 852. spfa判断负环 边权可能为负数。
#include <cstring> #include <iostream> #include <algorithm> #include <queue> ...
- Java读取、写入、处理Excel文件中的数据(转载)
原文链接 在日常工作中,我们常常会进行文件读写操作,除去我们最常用的纯文本文件读写,更多时候我们需要对Excel中的数据进行读取操作,本文将介绍Excel读写的常用方法,希望对大家学习Java读写Ex ...
- Ubuntu中获取和使用uiautomatorviewer
图省事的办法是直接在网上找个android platform tools的某个版本下载下来 比如这个网站里面的:Android SDK工具——Platform-tools 个人比较倾向于先下载andr ...
- python在线测试代码及教程
python/Java在线测试代码:http://www.pythontutor.com/visualize.html#mode=edit廖雪峰Python教学:https://www.liaoxue ...
- Java查询数据库
创建数据库 创建 user 数据库 创建 teacher 数据库 teacher表的user_id列与user表的id列建立一对多连接,user_id作为外键. Java编程查询数据库 向user数据 ...
- Vacation
题目看上去倒是蛮复杂的样子嘞!
- 每日扫盲(五):RPC(Remote Procedure Call)
作者:洪春涛链接:https://www.zhihu.com/question/25536695/answer/221638079来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...