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
;
}
大部分转载 小部分自写
查看全文
相关阅读:
ios qq 分享 失败
Collections在sort()简单分析法源
C# char[]与string之间的相互转换
uva 10837
良好的互联网站点
SVN库迁移
Android 它们的定义View它BounceProgressBar
#AOS应用基础平台# 添加了用户自己定义快捷菜单在平铺布局下的用户自己定义排序管理
android 逆向project smail 语法学习
Linux内核-系统调用
原文地址:https://www.cnblogs.com/8586/p/1493762.html
最新文章
Sql Server中清空所有数据表中的记录
java 对sql格式化
一台机器运行多个JBoss多实例
sql:除非另外还指定了 TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询
提高SQL查询效率 的10大方法
SQL Server的分页优化及Row_Number()分页存在的问题
SQL Server中TOP子句可能导致的问题以及解决办法
过滤器链chain.doFilter(request,response)含义
http返回写入问题
系统性能不够原因可能是cpu不够,内存不够等等
热门文章
linux sudo命令
Java泛型的主要用途
maven入门
maven学习(上)- 基本入门用法
解决 Eclipse 项目有红感叹号的方法
解决maven Generating project in Interactive mode
Maven创建项目时出现Generating project in Interactive mode就一直卡住的解决方案
Java 螺纹第三版 第三章数据同步 读书笔记
iphone分辨率终极指南(含有iphone6/6+)
Bootstrap "row"类宽度超过问题
Copyright © 2011-2022 走看看