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
;
}
大部分转载 小部分自写
查看全文
相关阅读:
android常用的Application类
Android一些问题的解决方案
MakeFile相关
Android源码与设计模式之notifyDataSetChanged()方法与观察者模式
Activity启动模式与onNewIntent()简述
(转)eval与迭代
ADB命令
其他常用工具类
文件操作常用工具方法
[TJOI2007] 可爱的质数
原文地址:https://www.cnblogs.com/8586/p/1493762.html
最新文章
笔记
JQ,JS笔记
遇见的错误
10--C++多态
08--MOOC--C/C++ 根据年月日计算星期几
09--c++ 类的继承与派生
08--C++拷贝构造函数详解
07--c++类的构造函数详解
06--谈谈:C++类的“包含”机制
读书笔记之:C++ Primer (第4版)及习题(ch12-ch18) [++++]
热门文章
读书笔记之:C++ Primer (第4版)及习题(ch01-ch11) [++++]
06--c++友元类
05-- C++ 类的静态成员详细讲解
浏览器渲染引擎介绍(备忘)
HTML不同按钮的点击效果不同(创)
Css嵌套DIV,内层DIV设置margin-top失效的解决办法(转)
C# 形参 实参 调用时在托管栈和托管堆中的分配情况
WebBrowser控件小知识
C# Thread 多线程 Monitor 锁 Producer And Consumer 生产者和消费者 经典模型
git与eclipse相关
Copyright © 2011-2022 走看看