zoukankan
html css js c++ java
模仿写的cstring类,操作符没有完全重载(=,)
//
String.cpp : 定义控制台应用程序的入口点。
//
#include
"
stdafx.h
"
template
<
typename Ty
>
class
CMyString
{
public
:
typedef unsigned size_t;
typedef Ty valuetype;
enum
{
MAX_LENGHT
=
1000
}
;
private
:
size_t m_uCout;
Ty m_Buf[MAX_LENGHT];
public
:
CMyString();
~
CMyString();
CMyString(
const
Ty
*
str);
CMyString(
const
CMyString
<
Ty
>&
other);
CMyString
&
operator
=
(
const
CMyString
<
Ty
>&
other);
CMyString
&
operator
+=
(
const
CMyString
<
Ty
>&
other);
CMyString
operator
+
(
const
CMyString
<
Ty
>&
other);
CMyString
&
operator
-=
(
const
CMyString
<
Ty
>&
other);
CMyString
operator
-
(
const
CMyString
<
Ty
>&
other);
public
:
size_t size()
{
return
m_uCout;
}
const
Ty
*
c_str()
const
{
return
m_Buf;
}
private
:
size_t mylen(
const
Ty
*
str)
{
if
(NULL
==
str)
return
0
;
size_t size
=
0
;
while
(
*
(str
+
size)
!=
0
)
{
++
size;
}
return
size;
}
void
mycpy(Ty
*
des,size_t size,
const
Ty
*
res)
{
size_t tmp
=
0
;
while
(tmp
<=
size
&&
*
(res
+
tmp)
!=
0
)
{
*
(des
+
tmp)
=
*
(res
+
tmp);
tmp
++
;
}
*
(des
+
tmp)
=
0
;
}
void
mycat(Ty
*
des,size_t size,
const
Ty
*
res)
{
size_t i
=
0
;
while
(
*
(des
+
i)
!=
0
)
{
i
++
;
}
size_t j
=
0
;
while
(
*
(res
+
j)
!=
0
)
{
*
(des
+
i
+
j)
=
*
(res
+
j);
j
++
;
}
*
(des
+
i
+
j)
=
0
;
}
}
;
//
默认构造函数
template
<
typename Ty
>
CMyString
<
Ty
>
::CMyString()
{
*
m_Buf
=
0
;
m_uCout
=
0
;
}
//
析构函数
template
<
typename Ty
>
CMyString
<
Ty
>
::
~
CMyString()
{
}
//
a("a")
template
<
typename Ty
>
CMyString
<
Ty
>
::CMyString(
const
Ty
*
other)
{
if
(other
==
NULL)
{
*
m_Buf
=
0
;
m_uCout
=
0
;
}
else
{
m_uCout
=
mylen(other);
mycpy(m_Buf,m_uCout,other);
}
}
//
a(b)
template
<
typename Ty
>
CMyString
<
Ty
>
::CMyString(
const
CMyString
<
Ty
>&
other):m_uCout(other.m_uCout)
{
mycpy(m_Buf,m_uCout,other.m_Buf);
}
//
c=b
template
<
typename Ty
>
CMyString
<
Ty
>&
CMyString
<
Ty
>
::
operator
=
(
const
CMyString
<
Ty
>&
other)
{
m_uCout
=
other.m_uCout;
mycpy(m_Buf,m_uCout,other.m_Buf);
return
*
this
;
}
//
c+=b
template
<
typename Ty
>
CMyString
<
Ty
>&
CMyString
<
Ty
>
::
operator
+=
(
const
CMyString
<
Ty
>
&
other)
{
m_uCout
+=
other.m_uCout;
mycat(m_Buf,other.m_uCout,other.m_Buf);
return
*
this
;
}
typedef CMyString
<
char
>
cstring;
int
_tmain(
int
argc, _TCHAR
*
argv[])
{
cstring a(
"
aaa
"
);
cstring b(a);
cstring c
=
b;
c
=
"
ccc
"
;
c.c_str();
cstring d
=
"
ddd
"
;
d
+=
c;
d
+=
"
abcde111
"
;
return
0
;
}
大部分转载 小部分自写
查看全文
相关阅读:
jmeter获取当前时间、时间运算、时间比较、时间转换
jmeter实现sha256算法加密
用FinalShell连接linux服务器
dstat:except getopt.error, exc:
新书上线:《Spring Boot+Spring Cloud+Vue+Element项目实战:手把手教你开发权限管理系统》,欢迎大家买回去垫椅子垫桌脚
Spring Cloud 教程
使用VMware Workstation Player虚拟机安装Linux系统
Java并发编程:Java实现多线程的几种方式
Java并发编程:Java中的锁和线程同步机制
Spring Boot:使用Rabbit MQ消息队列
原文地址:https://www.cnblogs.com/8586/p/1493762.html
最新文章
【Base】死锁产生的四个必要条件
【计算机网络】OSI七层模型图解
【数据库】分库分表策略
【数据库】事务的四大特性
【数据库】索引的优点和缺点
【Java】类加载过程
【LeetCode】二叉搜索树的前序,中序,后续遍历非递归方法
【LeetCode】LRU Cache
(学习必备)被虚拟机折腾够了?速来抢一台云服务器
性能测试:从0到实战(持续更新中。。。)
热门文章
性能测试常用术语解释
接口自动化测试实战(更新完毕)
【测试提升圈】低成本投资自己,提升职场竞争力;洞悉性能的本质,享受测试的乐趣
测试技术提升建议(必看)
【自学目录】从测试小白到高级全栈测试修炼之路,持续更新中。。。关注公众号『全栈测试笔记』,第一时间获取干货分享
你也可能遇到的一个类似的性能需求
pidstat
htop
loadrunner查看并发产生的错误日志
jmeter压测tcp协议接口:java.net.SocketException: Software caused connection abort: socket write error
Copyright © 2011-2022 走看看