CString的高效版本
发布日期:2021-07-14 20:03:42
浏览次数:4
分类:技术文章
本文共 2369 字,大约阅读时间需要 7 分钟。
#pragma once#includetemplate void SwapFun(T& left, T& right){ T temp = left; left = right; right = temp;}class CShString{public: CShString(char* pStr) :m_iLen(strlen(pStr)) , m_iMaxSize(CalcuMax(m_iLen)) , px(new char[m_iMaxSize]) , pn(new int(1)) { memcpy(px, pStr, m_iLen + 1); } CShString(char* pStr, int iLength) :m_iLen(iLength) , m_iMaxSize(CalcuMax(iLength)) , px(new char[m_iMaxSize]) , pn(new int(1)) { ASSERT(iLength == strlen(pStr)); memcpy(px, pStr, m_iLen); px[m_iLen] = '\0'; } CShString(const CShString& strOther) : px (strOther.px), pn(strOther.pn); { ++*pn; } CShString& Assign(const CShString& other) { Swap(CShString(other.Data(), other.GetLength())); return *this; } CShString& Assign(const char* pStr) { Swap(CShString(pStr)); return *this; } CShString& Assign(const char* pStr, int ilength) { Swap(CShString(pStr, ilength)); return *this; } CShString& Append(const CShString& other) { int nSize = GetLength() + other.GetLength() + 1; char* pStr = new char[nSize]; memcpy(pStr, Data(), GetLength()); memcpy(pStr + GetLength(), other,Data(), other.GetLength() + 1); ResetAs(pStr, nSize); } CShString& Append(const char* pOther) { int nLen = strlen(pOther); int nSize = GetLength() + nLen + 1; char* pStr = new char[nSize]; memcpy(pStr, Data(), GetLength()); memcpy(pStr + GetLength(), pOther, nLen + 1); ResetAs(pStr, nSize); } CShString& Append(const char* pStr, int ilength) { int nSize = GetLength() + ilength + 1; char* pStr = new char[nSize]; memcpy(pStr, Data(), GetLength()); memcpy(pStr + GetLength(), pStr, ilength + 1); ResetAs(pStr, nSize); } ~CShString(void) { if (--*pn == 0) { delete pn; delete px; } } const char* Data() const { return px; } int GetLength() { return m_iLen; }private: void ResetAs(char* pStr, int iSize) { if (--*pn == 0) { delete pn; delete px; } m_iMaxSize = CalcuMax(iSize); m_iLen = iSize - 1; px = pStr; pn = new int(1); } void Swap(CShString& other) { SwapFun(m_iLen, other.m_iLen); SwapFun(m_iMaxSize, other.m_iMaxSize); SwapFun(px, other.px); SwapFun(pn, other.pn); } static int CalcuMax(int iLen) { int i = 0; while (INIT_MAX_LEN + (i++) * DELTA_LEN < iLen + 1){} return INIT_MAX_LEN + (i - 1) * DELTA_LEN; } static const int INIT_MAX_LEN = 100; static const int DELTA_LEN = 20; int m_iLen; int m_iMaxSize; char* px; int* pn;};
转载地址:https://blog.csdn.net/daojin505/article/details/76812745 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年03月30日 23时17分35秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
C#用正则表达式去匹配被双引号包起来的中文
2019-04-27
lua table排序
2019-04-27
Unity发布的ios包在iphone上声音是从听筒里出来的问题
2019-04-27
UIScrollView复用节点示例
2019-04-27
Unity 5 AudioMixer
2019-04-27
Unity 代码混淆: CodeGuard的使用
2019-04-27
UGUI 列表循环使用
2019-04-27
使用命令行运行unity并执行某个静态函数(运用于命令行打包和批量打包)
2019-04-27
web.py框架
2019-04-27
web.py学习笔记
2019-04-27
python的代码缩进
2019-04-27
A* Pathfinding Project (Unity A*寻路插件) 使用教程
2019-04-27
bash学习笔记
2019-04-27
sqlite学习
2019-04-27
手把手教你实现Unity与Android的交互
2019-04-27
手把手教你使用Unity的Behavior Designer
2019-04-27
Unity3D摄像机裁剪——NGUI篇
2019-04-27
lua深拷贝一个table
2019-04-27
app运行提示Unable to Initialize Unity Engine
2019-04-27
spring boot 与 Ant Design of Vue 实现修改按钮(十七)
2019-04-27