常见string.h库函数实现 及 string类实现

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;
}

void *_memmove(void* dest, const void* src, size_t count)
{
	assert(src != nullptr&&dest != nullptr);
	//判断dest指针和src指针是否为空,若为空抛出异常
	char* tmp_dest = (char*)dest;
	const char* tmp_src = (const char*)src;
	
	if (tmp_src < tmp_dest)//当src地址小于dest地址时,从头进行拷贝
		while (count--)
			*tmp_dest++ = *tmp_src++;
	else if (tmp_src > tmp_dest)//当src地址大于dest地址时,从后进行拷贝
	{
		tmp_src += count - 1;
		tmp_dest += count - 1;
		while (count--)
			*tmp_dest-- = *tmp_src;
	}
	//else(tmp_src==tmp_dest) 此时不进行任何操作
	return dest;
}

int _strlen(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;
}



你可能感兴趣的:(C,plus,plus,C)