【C++】初始化函数memset的实现,以及memcpy/memmove/memset/strcpy/my_strcpy/strlen/strcat/strcmp实现代码测试

原型

void * memset ( void * ptr, int value, size_t num );

功能

这个函数通常为新申请的内存做初始化工作。
Fill block of memory
Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char).

参数

ptr
Pointer to the block of memory to fill.
value
Value to be set. The value is passed as an int, but the function fills the block of memory using the unsigned char conversion of this value.
num
Number of bytes to be set to the value.
size_t is an unsigned integral type.

例子

/* memset example */
#include <stdio.h>
#include <string.h>
int main ()
{
  char str[] = "almost every programmer should know memset!";
  memset (str,'-',6);
  puts (str);
  return 0;
}

output:------ every programmer should know memset!

实现

void * memset(void * ptr, int value, size_t num)
{
	char* ptr_func = (char*)ptr;
	if (ptr == nullptr || num < 0)
		return nullptr;
	while (num--) {
		*ptr_func++ = value;
	}
	return ptr;
}

memcpy/memmove/memset/strcpy/my_strcpy/strlen/strcat/strcmp实现代码测试

#include <iostream>
#include <vector>
#include <stack>
#include <string>
#include <assert.h>
using namespace std;
class Solution {
public:
	//移动内存
	void* memcpy(void* dest, const void* src, size_t n);
	void* memmove(void* dest, const void* src, size_t n);
	
	void * memset(void * ptr, int value, size_t num);

	char* strcpy(char* dest, const char* src);
	char* my_strcpy(char* dest, const char* src);
	size_t strlen(const char* str);
	char* strcat(char* dest, const char* src);
	int strcmp(const char* str1, const char* str2);
};

int main()
{
	Solution sol;
	char str1[20] = "hello world";
	char str2[40], str3[40], str4[40];

	sol.memcpy(str2, str1, sizeof(str1) / sizeof(str1[0]));
	sol.memmove(str3, str1, sizeof(str1) / sizeof(str1[0]));
	sol.strcpy(str4, str1);

	//sol.strcpy(str1, str1 + 1);
	//sol.my_strcpy(str1 + 1, str1);
	char str5[20] = "hello world";
	int len = sol.strlen(str1);
	sol.strcat(str1, str5);
	int cmp = sol.strcmp(str1, str5);
	sol.memset(str1, 0, strlen(str1)/sizeof(str1[0]));

	cout << str2 << endl << str3 << endl << str4;
	cout << endl;
	system("pause");
	return 0;
}

//无需考虑内存重叠问题
void * Solution::memcpy(void * dest, const void * src, size_t n)
{
	char* d = (char*)dest;
	const char* s = (const char*) src;
	while (n--)
	{
		*d++ = *s++;
	}
	return dest;
}

void * Solution::memmove(void * dest, const void * src, size_t n)
{
	char* d = (char*)dest;
	const char* s = (const char*)src;

	if (s > d)//源地址在后,从前往后
	{
		while (n--)
			*d++ = *s++;
	}
	if (s < d)//源地址在前,从后往前
	{
		d = d + n - 1;
		s = s + n - 1;
		while (n--)
		{
			*d-- = *s--;
		}
	}
	return dest;
}

void * Solution::memset(void * ptr, int value, size_t num)
{
	char* ptr_func = (char*)ptr;
	if (ptr == nullptr || num < 0)
		return nullptr;
	while (num--) {
		*ptr_func++ = value;
	}
	return ptr;
}

char * Solution::strcpy(char * dest, const char * src)
{
	assert(dest != nullptr && src != nullptr);
	char * address = dest;
	while ((*dest++ = *src++) != '\0')
		;
	return address;
}

char * Solution::my_strcpy(char * dest, const char * src)
{
	assert(dest != nullptr && src != nullptr);
	char *d = dest;
	int srcLen = strlen(src) + 1;//加上'\0'
	if (d > src)//从后往前拷贝
	{
		d = d + srcLen - 1;
		src = src + srcLen - 1;
		while (srcLen--)
			*d-- = *src--;
	}
	if (d < src)//从前往后拷贝
	{
		while (srcLen--)
			*d++ = *src++;
	}
	//d = src时不拷贝,直接返回
	return d;
}

size_t Solution::strlen(const char * str)
{
	assert(str != nullptr);
	int len = 0;
	while (*str++ != '\0')
	{
		len++;
	}
	return len;
}

char * Solution::strcat(char * dest, const char * src)
{
	assert(dest != nullptr && src != nullptr);
	char *d = dest;
	while (*d != '\0')//移动字符串到末尾
		d++;
 	while (*src != '\0')
	{
		*d++ = *src++;
	}
	return d;
}

int Solution::strcmp(const char * str1, const char * str2)
{
	assert(str1 != nullptr && str2 != nullptr);
	while (*str1 == *str2)//不等或者遇到'\0'时返回结果
	{
		if (*str1 == '\0')
			return 0;
		str1++;
		str2++;
	}
	return *str1 - *str2;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值