常見string.h庫函式實現 及 string類實現
阿新 • • 發佈:2018-12-21
String.h 庫函式實現
void *Memcpy(char *dst, char const *src, int len)
{
assert(dst && src);
char *tmp = dst;
const char *s = src;
while (len--)
{
*dst++ = *src;
}
return tmp;
}
int mStrlen(const char *str)
{
assert(str);
int len = 0;
while ((*str++) != '\0')len++;
return len;
}
char *Strcpy(char *dst, const char *src)
{
assert(dst && src);
char *tmp = dst;
while ((*dst++ = *src++) != '\0');
return tmp;
}
char *Strncpy(char *dst, const char *src, int len)
{
assert(dst&&src);
char *tmp = dst;
int offset = 0;
if (len > strlen(src))
{
offset = len - strlen (src);
len = strlen(src);
}
while (len--)
{
*dst++ = *src++;
}
while (offset--)
{
*dst++ = '\0';
}
return tmp;
}
char *Strcat(char *dst, const char* src)
{
assert(dst && src);
char *tmp = dst;
while (*dst++);
dst--;
while (*dst++ = *src++);
return tmp;
}
char *Strncat(char *dst, const char* src, int len)
{
assert(dst && src);
char *tmp = dst;
while (*dst++);
dst--;
while (len--)
{
*dst++ = *src++;
}
*dst = '\0';
return tmp;
}
int Strcmp(char const *s1, char const *s2)
{
assert(s1&&s2);
while (*s1 == *s2 && *s1 != '\0' && *s2 != '\0')
{
s1++;
s2++;
}
if (*s1 == *s2)
{
return 0;
}
else if (*s1 > *s2)
{
return 1;
}
return -1;
}
int Strncmp(char const *s1, char const *s2, int len)
{
assert(s1&&s2);
while (len-- && *s1 == *s2 && *s1 != '\0' && *s2 != '\0')
{
s1++;
s2++;
}
return *s1 - *s2;
}
char *Strpbrk(char const *str, char const *group)
{
assert(str&&group);
const char *ss, *sg;
for (ss = str; *ss != '\0'; ss++)
{
for (sg = group; *sg != '\0'; sg++)
{
if (*ss == *sg)
{
return (char*)ss;
}
}
}
return NULL;
}
char *Strstr(char const *s1, char const *s2)
{
assert(s1&&s2);
const char *ps1, *ps2;
ps1 = s1;
ps2 = s2;
while (*ps1)
{
const char *tmp = ps1;
while (*tmp++ == *ps2++);
if (*ps2 == '\0')
{
return (char*)ps1;
}
ps2 = s2;
ps1++;
}
return NULL;
}
string類 實現
class String
{
friend ostream& operator<<(ostream &out, String const &s);
friend istream& operator>>(istream &in, String &s);
public:
String(const char *str = "");
String(const String & s);
~String();
String operator+(const String & s);
String &operator=(const String & s);
String &operator=(const char* s);
char &operator[](int index);
const char & operator[](int index) const;
bool operator>(const String & s);
bool operator<(const String & s);
bool operator==(const String & s);
private:
char *m_pStr;
};
String::String(const char *str)
:m_pStr(new char[strlen(str) + 1])
{
strcpy(m_pStr, str);
}
String::String(const String & s)
{
//m_pStr = new char[strlen(s.m_pStr) + 1];
//strcpy(m_pStr, s.m_pStr);
String tmp(s.m_pStr); //現代寫法
swap(m_pStr, tmp.m_pStr);
}
String::~String()
{
delete[]m_pStr;
m_pStr = NULL;
}
String String::operator+(const String & s)
{
String *newStr = new String;
int len = strlen(m_pStr) + strlen(s.m_pStr);
newStr->m_pStr = new char[len + 1];
strcpy(newStr->m_pStr, m_pStr);
strcat(newStr->m_pStr, s.m_pStr);
//delete[]m_pStr; 錯誤!!! 不是放自身空間 只有 operator+= 才釋放自身
//m_pStr = NULL;
return *newStr;
}
String &String::operator=(const String & s)
{
if (this == &s)
{
return *this;
}
//delete m_pStr;
//m_pStr = new char[strlen(s.m_pStr) + 1];
//strcpy(m_pStr, s.m_pStr);
String tmp(s.m_pStr); //現代寫法
swap(m_pStr, tmp.m_pStr);
return *this;
}
String &String::operator=(const char* str)
{
//delete m_pStr;
//m_pStr = new char[strlen(str) + 1];
//strcpy(m_pStr, str);
String tmp(str); //現代寫法
swap(m_pStr, tmp.m_pStr);
return *this;
}
char &String::operator[](int index)
{
return m_pStr[index];
}
const char &String::operator[](int index) const
{
return m_pStr[index];
}
bool String::operator<(const String & s)
{
char *s1 = m_pStr;
char *s2 = s.m_pStr;
while (*s1 == *s2 && *s1 != '\0' && *s2 != '\0')
{
*s1++;
*s2++;
}
return ((*s1 - *s2) < 0) ? true : false;
}
bool String::operator>(const String & s)
{
return (strcmp(m_pStr, s.m_pStr) > 0) ? true : false;
}
bool String::operator==(const String & s)
{
if (!operator<(s) && !operator>(s))
{
return true;
}
return false;
}
ostream& operator<<(ostream &out, String const &s)
{
out << s.m_pStr;
return out;
}
istream& operator>>(istream &in, String &s)
{
in >> s.m_pStr;
return in;
}