头文件:
#ifndef _MYSTRING_H_
#define _MYSTRING_H_
#include
namespace dj
{
class mystring
{
private:
char* buff;
public:
mystring();
mystring(const char* str);
mystring(const mystring& str);
virtual ~mystring();
int size() const;
bool empty() const;
const char* c_str() const;
void append(char c);
void append(const char* str);
void append(int i);
void insert(unsigned int pos, char c);
void insert(unsigned int pos, const char* str);
bool remove(char c);
bool remove(const char* str);
bool update(char old_c, char new_c);
int find(char c);
int find(const char* str);
mystring& operator=(const mystring& str);
bool operator==(const mystring& str);
mystring operator+(const mystring& str);
char& operator[](int i);
friend std::ostream& operator<<(std::ostream& out, const mystring& s);
friend std::istream& operator>>(std::istream& in, mystring& s);
};
}
#endif
源文件:
#include
#include
#include
#include "mystring.h"
namespace dj
{
mystring::mystring()
{
this->buff = new char{'}';}
::
mystringmystring(constchar *) str=
{
this->buff char new [strlen()str+ 1 ];strcpy
(,this->buff) str;}
::
mystringmystring(const& mystring) str=
{
this->buff char new [strlen(.str)buff+ 1 ];strcpy
(,this->buff. str)buff;}
::
mystring~mystring()if
{
(!=this->buff ) nullptr[
delete]; this->buff}
int
:: mystringsize()const int
{
= cnt 0 ;while
([this->buff]cnt!= ')' ++; returncnt;
} cnt::
empty
bool mystring()constreturn [
{
0 this->buff]==';' } constchar
*
:: c_str( mystring)constreturn; }
{
void this->buff::
append
( mystringchar)char* c=
{
char[ tmp strlen new ()+1this->buff+ 1 ] ; sprintf(,
"%s%c",tmp, ); this->buff[ c];
delete=; this->buff}
this->buff void tmp::
append
( mystringconstchar*) char* str=
{
char[ tmp strlen new ()+strlenthis->buff( ) +1str] ; sprintf(,
"%s%s",tmp, ); this->buff[ str];
delete=; this->buff}
this->buff void tmp::
append
( mystringint)char[ i15
{
] num;sprintf(,
"%d",num) ;append i()
this->;}numvoid::
insert
( mystringunsignedint,char ) posif ( cstrlen
{
()pos > )=this->buffstrlen(
pos ) ;charthis->buff*=
char[ tmp strlen new ()+1this->buff+ 1 ] ; strncpy(,
,)tmp; this->buffsprintf pos(+
,"%c%s"tmp , pos, +) c; this->buff [ pos];
delete=; this->buff}
this->buff void tmp::
insert
( mystringunsignedint,const char pos* ) if( strstrlen
{
()pos > )=this->buffstrlen(
pos ) ;charthis->buff*=
char[ tmp strlen new ()+strlenthis->buff( ) +1str] ; strncpy(,
,)tmp; this->buffsprintf pos(+
,"%s%s"tmp , pos, +) str; this->buff [ pos];
delete=; this->buff}
this->buff :: tmpremove
(
bool mystringchar)int; cfor
{
( pos=
0;pos [ ]!= this->buff'&&'pos[ ] != ; this->buff++pos) ; cif pos([]
==')'this->buffreturnpos; do [] = false[
+
{
this->buff1pos] ; this->buff++pos ; }while(
pos[]
!= ')' ;this->buffreturnpos; } ::remove(
const truechar
*
bool mystring)char*= strstr( str,
{
); p if (==this->buff) strreturn;
int=nullptr - p; int false=
strlen pos ( p ) this->buff;
do len [ ]=str[+
]
{
this->buff;pos++ ; this->buff}pos while len([
pos]!=
')' ; returnthis->buff;pos} :: update(char
, truechar
)
bool mystringint;for( old_c= 0 new_c;
{
[ pos]
!='&&'pos [ ]!= this->buff;pos++ ) ; if this->buff(pos[ ] old_c== pos')'return;
[]this->buff=pos; return ;} int false::
this->bufffindpos( char new_c)
int true;
for
( mystring=0;[ c]
{
!= pos';'
++)pos if ([ this->buff]pos== ) return; pos}return
{
-1this->buff;pos} int c:: find pos(
const
char *)char
*
= mystringstrstr(,) ;if str(
{
==) p return -1this->buff; strreturn-
;}nullptr & p:: = (const&
) p [ this->buff]
;
mystring= mystringcharoperator[strlen( mystring. str)
{
delete+1 this->buff]
this->buff ; new strcpy(,.str)buff; return *;}
::==this->buff( strconstbuff&)
return !thisstrcmp
(
bool mystring,operator.); mystring} str::
{
+ (const&this->buff) str;buff.append
(
mystring mystring)operator;.append mystring( str.
{
mystring tmp)
tmp;return;this->buff}char
tmp&::[str]buff(int
) tmpreturn
[
]; mystring}operator::&<<( i::
{
& this->buff,iconst&
)
std<<ostream. operator;returnstd;ostream} out:: & mystring( s::
{
out & s,buff&
) outchar
[
std1025istream] operator>>;std.istreamgetline in( mystring, ssizeof
{
( tmp));[
in].;tmp. =chartmp[strlen(
delete)+ s1buff]
s;buff strcpy new (.,)tmp; return ;}}
#includes#buffinclude tmp"mystring.h"#
include in;
int
main
调用文件:
()
// mystring s1;// mystring s2("abcd"); // mystring s3 = "123";
// s1 = "hello";// cout << s1.size() << endl;
using namespace std// cout << s2.size() << endl;
// cout << s3.size() << endl; // s3 = "";// if(s3.empty())// cout << "s3 is empty!" << endl;
{
// }
// s1.append(',');
// s1.append("world");
// s1.append(123);
// s1.insert(0, "haha ");
// s1.insert(5, "你好 ");
// s1.remove(' ');
// s1.remove("123");
// if(!s1.remove("432"))
// {
// cout << "删除失败!" << endl;
// }
// cout << s1 << endl;
// while(s1.update('a', 'b'));
// cout << s1 << endl;
// mystring userName;
// cout << "用户名:";
// if(userName == "zhangsan")
// cout << "用户名正确!" << endl;
// }
// {
// printf("%s\n", userName.c_str());
// s1 = s2 + s3 + "abc";
// s1 = mystring("abc") + "def";
// cout << s1 << endl;
// string s = "abcdef";
// const char* p = s.c_str();
// for(int i = 0; i < s.size(); i++)
// cin >> userName;
// cout << s[i] << endl;
// {
// s[2] = 'm';
// cout << s << endl;
;
=
"fdsafdlfldasfkljdsafkljdsafkljdsafkljdsafd;asfdasf"
;
<<
sizeof
(
)
<<
;
using namespace dj<<
mystring s . size(
cout ) <<;sreturn 0 endl;
cout } s endl
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)