实现C++标准库string类的简单版本
代码如下:
#ifndef STRING_H
#define STRING_H #include <cassert>
#include <utility>
#include <iostream> namespace jz
{ /************************************************************************/
/* 重新实现C风格字符串处理函数 */
/************************************************************************/ //求C风格字符串长度
size_t StrLen(const char *str)
{
assert(str != nullptr);
size_t len = ;
while (*str++ != '\0')
{
++len;
}
return len;
} //复制C风格字符串
char* StrCpy(char *dest, const char *src)
{
assert(dest != nullptr && src != nullptr);
char *temp = dest;
while ((*temp++ = *src++) != '\0');
return dest;
} //复制指定长度字符串
char* StrNCpy(char *dest, const char *src, size_t n)
{
assert(dest != nullptr && src != nullptr);
char *temp = dest;
while (n-- && (*temp++ = *src++) != '\0');
if (*temp != '\0')
{
*temp = '\0';
}
return dest;
} //拼接字符串
char* StrCat(char *dest, const char *src)
{
assert(dest != nullptr && src != nullptr);
char *temp = dest;
while (*temp != '\0')
{
++temp;
}
while ((*temp++ = *src++) != '\0');
return dest;
} //比较字符串
int StrCmp(const char *lhs, const char *rhs)
{
assert(lhs != nullptr && rhs != nullptr);
int ret = ;
while (!(ret = *lhs - *rhs) && *rhs)
{
++lhs;
++rhs;
}
if (ret > )
{
return ;
}
else if (ret < )
{
return -;
}
else
{
return ;
}
} /************************************************************************/
/* 实现标准库String类 */
/************************************************************************/
class String
{
//重载==运算符
friend bool operator==(const String &lhs, const String &rhs)
{
if (StrCmp(lhs.str, rhs.str) == )
{
return true;
}
else
{
return false;
}
} //重载!=运算符
friend bool operator!=(const String &lhs, const String &rhs)
{
return !(lhs == rhs);
} //重载+运算符
friend String operator+(const String &lhs, const String &rhs)
{
String temp;
delete[] temp.str;
temp.str = new char[lhs.Size() + rhs.Size() + ];
StrCpy(temp.str, lhs.CStr());
StrCat(temp.str, rhs.CStr());
return temp;
} //重载<<运算符
friend std::ostream& operator<<(std::ostream &os, const String &rhs)
{
os << rhs.str;
return os;
} public:
//默认构造函数
String() : str(new char[])
{
*str = '\0';
} //C风格字符串作为参数的构造函数
String(const char *cstr) : str(new char[StrLen(cstr) + ])
{
StrCpy(str, cstr);
} //拷贝构造函数(委托构造函数)
String(const String &rhs) : String(rhs.str)
{ } //拷贝赋值运算符,使用传值参数,保证异常安全并可自我赋值
String& operator=(String rhs)
{
Swap(rhs);
return *this;
} //析构函数
~String()
{
delete[] str;
} //重载+=运算符
String& operator+=(const String &rhs)
{
*this = *this + rhs;
return *this;
} //重载[]运算符
char& operator[](size_t i)
{
assert(i >= && i <= StrLen(str));
return str[i];
} //求字符串长度
size_t Size() const
{
return StrLen(str);
} //返回C风格字符串
const char* CStr() const
{
return str;
} //交换
void Swap(String &rhs)
{
std::swap(str, rhs.str);
} private:
char *str;
}; } #endif
实现C++标准库string类的简单版本的更多相关文章
- 标准库String类
下面的程序并没有把String类的所有成员方法实现,只参考教程写了大部分重要的成员函数. [cpp] view plain copy #include<iostream> #include ...
- C++标准库<string>简单总结
C++标准库<string>简单总结 在C++中,如果需要对字符串进行处理,那么它自带的标准库<string>无疑是最好的选择,它实现了很多常用的字符处理函数. 要想使用标准C ...
- C++标准库异常类
C++标准库异常类 2012-12-24 16:27 5269人阅读 评论(1) 收藏 举报 分类: c/c++(36) C++标准库异常类继承层次中的根类为exception,其定义在excep ...
- C++异常第二篇---C++标准库异常类exception的使用
1 继承图示 2 具体讲解 C++标准库异常类继承层次中的根类为exception,其定义在exception头文件中,它是C++标准库所有函数抛出异常的基类,exception的接口定义如下: na ...
- 【C++ Primer每日刷】之三 标准库 string 类型
标准库 string 类型 string 类型支持长度可变的字符串.C++ 标准库将负责管理与存储字符相关的内存,以及提供各种实用的操作.标准库string 类型的目的就是满足对字符串的一般应用. 与 ...
- C++标准库string类型
string类型支持长度可变的字符串,C++标准库将负责管理与存储字符相关的内存,以及提供各种有用的操作.标准库string类型的目的就是满足对字符串的一般应用. 本文地址:http://www.cn ...
- C++标准库string
C++标准库string 定义和初始化 string s1 默认初始化,s1是一个空串 string s2(s1) s2是s1的副本 string s2 = s1 等价于s2(s1),s2是s1的副本 ...
- c/c++ 标准库 string
c/c++ 标准库 string 标准库 string的小例子 test1~test10 #include <iostream> using namespace std; int main ...
- C 标准库 - string.h
C 标准库 - string.h This header file defines several functions to manipulate C strings and arrays. stri ...
随机推荐
- HDU5124:lines(线段树+离散化)或(离散化思想)
http://acm.hdu.edu.cn/showproblem.php?pid=5124 Problem Description John has several lines. The lines ...
- Python---2. 函数
转载: Py西游攻关之函数 补充: map函数和reduce函数的区别
- Integration Services 变量
如果没有变量,你会发现在ssis里面啥都干不成,和人没有灵魂一样 对系统变量唯一可配置的选项是指定变量在更改值时是否引发事件. 待续
- 1:3访问 servlet API 的两种方式(request,session等内置对象)
1:解耦方式 2:耦合方式: ========================================== ========================================== ...
- 120. Triangle(动态规划 三角形最小路径 难 想)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- 使用Linux重定向解决nohup.out无写权限问题
■场景 执行nohup命令的时候,经常会出现下面这种没有写入权限的错误. nohup: ignoring input and appending output to `nohup.out'nohup: ...
- 图像处理(二十一)基于数据驱动的人脸卡通动画生成-Siggraph Asia 2014
http://blog.csdn.net/garfielder007/article/details/50582018 在现实生活中,我们经常会去评价一个人,长得是否漂亮.是不是帅哥美女,然而如何用五 ...
- Python笔记 #17# Pandas: Merge
10 Minutes to pandas Concat df = pd.DataFrame(np.random.randn(10, 4)) print(df) # break it into piec ...
- mysql 触发器 trigger用法 one (简单的)
实例~~ example1: 创建表tab1 1 2 3 4 DROP TABLE IF EXISTS tab1; CREATE TABLE tab1( tab1_id varchar(11) ...
- Vue学习笔记之Vue知识点补充
0x00 修饰符 .lazy 在默认情况下,v-model 在每次 input 事件触发后将输入框的值与数据进行同步 .你可以添加 lazy 修饰符,从而转变为使用 change 事件进行同步: &l ...