C string類 模擬實現

2021-10-05 20:32:17 字數 3728 閱讀 9444

#define _crt_secure_no_warnings 

#include

#include

using

namespace std;

namespace zyf

iterator end()

mystring

(const

char

* str ="")

:_size

(strlen

(str))~

mystring()

//s1(s2)

mystring

(const mystring& s)

//s1 = s2

mystring&

operator=(

const mystring& s)

return

*this;}

size_t size()

const

size_t capacity()

const

char

&operator

(size_t pos)

const

char

&operator

(size_t pos)

const

void

reserve

(size_t n)

}void

resize

(size_t n,

char ch =

'\0'

)else

}'x');

void

push_back

(char ch)

_str[_size]

= ch;

++_size;

_str[_size]

='\0';}

void

(const

char

* str)

//s1 += 'x'

mystring&

operator+=

(char ch)

//s1 += "***xx"

mystring&

operator+=

(const

char

* str)

mystring&

insert

(size_t pos,

char ch)

_str[pos]

= ch;

++_size;

return

*this;}

mystring&

insert

(size_t pos,

const

char

* str)

strncpy

(_str + pos, str, len)

;//在pos位置拷貝len個位元組,不拷貝'\0'所以不用strcpy

_size +

= len;

return

*this;}

void

erase

(size_t pos, size_t len = npos)

else

} size_t find

(char ch, size_t pos =0)

return npos;

} size_t find

(const

char

* str, size_t pos =0)

//s1 < s2

//實現《和=,復用實現其他比較

bool

operator

<

(const mystring& s)

bool

operator==(

const mystring& s)

bool

operator

<=

(const mystring& s)

bool

operator

>

(const mystring& s)

bool

operator

>=

(const mystring& s)

bool

operator!=(

const mystring& s)

private

:char

* _str;

size_t _size;

size_t _capacity;

static size_t npos;};

size_t mystring::npos =-1

;//整形最大數

ostream&

operator

<<

(ostream& out,

const mystring& s)

istream&

operator

>>

(istream& in, mystring& s)

return in;}}

void

test1()

cout << endl;

zyf::mystring::iterator it1 = s1.

begin()

;//遍歷+讀寫

while

(it1 != s1.

end())

cout << endl;

for(

auto

& ch : s1)

cout << endl;

zyf::mystring s2

("hello");

s2 +

='x'

; s2.

resize(3

);s2.

resize(7

,'x');

s2.resize(15

,'x');

}void

test2()

intmain()

#define _crt_secure_no_warnings

#include

#include

using

namespace std;

namespace zyf

/*傳統深拷貝寫法

string(const string&s)

:_str(new char[strlen(s._str) + 1])

string& operator=(const string&s)

return *this;

}*/mystring

(const mystring& s)

//現**法

:_str

(nullptr

)//若自己給自己賦值,僅交換空間

mystring&

operator

=(mystring s)

/*mystring& operator=(const mystring &s)

return *this;

}*/~mystring()

const

char

*c_str()

private

:char

*_str;};

}/*template//交換變數自身

void swap(t&x1, t&x2) */

intmain()

C string類的模擬實現

include include using namespace std class string iterator end const iterator begin const const iterator end const string const char str 字串有 0 也可以寫成 0 ...

C string類介面及模擬實現

c語言中,字串是以 0 結尾的一些字元的集合,為了操作方便,c標準庫中提供了一些str系列的庫函式,但是這些庫函式與字串是分離開的,而且底層空間需要使用者自己管理,稍不留神可能還會越界訪問。c 封裝了這些介面和操作,建立出string類 string 是表示字串的類 該類的介面與常規容器的介面基本相...

C string的模擬實現

define crt secure no warnings include include using namespace std class string iterator end const iterator begin const const iterator end const 預設無參構造...