C语言string库函数介绍(附模拟实现)

目录

strlen

模拟实现

strcpy

模拟实现

strcat

模拟实现

strcmp

模拟实现

strncpy

strncat

strncmp

strstr

模拟实现


strlen

size_t strlen (const char * str)

字符串将'\0'作为结束标志,strlen函数返回的是在字符串中'\0'前面出现的字符个数(不包含'\0').

参数指向的字符串必须要以'\0'结束。

函数的返回值为size_t,是无符号的(unsigned int)

这里用strlen(str2) - strlen(str1)的值来判断字符串长度大小。明明str2只有三个字符,但是结果显示的是str2>str1。这里就是因为strlen返回的值都是size_t(无符号整形),两个无符号整形相减得到的结果也是无符号整形,结果本应该是-3,却因为是无符号整形改变了它的值,变成一个无穷大的数。这就是为什么输出结果是str2 > str1.简单的解决方法是在前面强制类型转换成int即可。

模拟实现

 

strcpy

char* strcpy(char * destination, const char * source)

Copies the C string pointed by source into the array pointed by destination, including the terminating null character(and stopping at that point)

源字符串必须以'\0'结束。

会将源字符串中的'\0'拷贝到目标空间。

目标空间必须足够大,以确保能存放源字符串。

目标空间必须可变。

模拟实现

 

strcat

char * strcat ( char * destination, const char * source)

Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination.

源字符串必须以'\0'结束。

目标空间必须足够大,能容纳下源字符串的内容。

目标空间必须可修改。

模拟实现

 

strcmp

int strcmp( const char * str1,const char * str2)

This function starts comparing the first character of each string. If they r equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached.

标准规定:

第一个字符串大于达尔戈字符串,则返回大于0的数字。

第一个字符串等于第二个字符串,则返回0

第一个字符串小于第二个字符串,则返回小于0的数字

模拟实现

 

strncpy

char * strncpy( char * destination, const char * source, size_t num)

Copies the first num characters of source to destination. If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it.

拷贝num个字符从源字符串到目标空间

如果源字符串的长度小于num,则拷贝完源字符串之后,在目标后面追加‘\0’,直到num个。

strncat

char * strncat( char * destination, const char * source, size_t num)

Appends the first num characters of source to destination, plus a terminating null-character.

追加完毕后后在末尾添加'\0'

If the length of the C string in source is less than num, only the content up to the terminating null-character is copied.

如果num大于source string长度,不会额外追加。

strncmp

int strncmp( const char *string1, const char *string2, size_t count)

比较到出现另个字符不一样或者一个字符串结束或者num个字符全部比较完。

大体与strcmp相似,也是在末尾添加一个num,限制了比较数量。

strstr

char * strstr( const char * string, const char *strCharSet)

Return Value

Each of these functions returns a pointer to the first occurrence of strCharSet in string, or NULL if strCharSet does not appear in string.If strCharSet points to a string of zero length, the function returns string.

返回的是在string中首次出现strCharSet字符串的地址。如果没有在string中找到strCharSet字符串,返回NULL。或者strCharSet字符串的长度为0,函数返回string。

模拟实现

 

 

### 使用DeepFace实现人脸检测 为了使用 `DeepFace` 库进行人脸检测,首先需要安装必要的依赖项。可以利用多种不同的人脸检测器来完成这一任务,如 OpenCV、RetinaFace、MTCNN、SSD、dlib 和 MediaPipe 等[^2]。 #### 安装依赖包 确保已经安装了所需的Python环境以及pip工具之后,在命令行执行如下指令: ```bash pip install deepface opencv-python-headless ``` 对于特定的人脸检测器(比如 RetinaFace),可能还需要额外的安装步骤: ```bash pip install retina-face ``` #### 导入所需模块并加载图像 接下来编写一段简单的脚本来展示如何调用人脸检测功能。这里假设有一个名为 "img.jpg" 的图片文件待处理。 ```python from deepface import DeepFace import cv2 # 加载测试图像 image_path = "path/to/img.jpg" img = cv2.imread(image_path) # 执行人脸检测,默认采用opencv作为检测器 detected_faces = DeepFace.detectFace(img_path=image_path, detector_backend='opencv') print(detected_faces) ``` 上述代码片段展示了基本的脸部识别流程,并选择了OpenCV作为默认的检测引擎。如果想要尝试其他类型的检测器,则可以通过修改参数 `detector_backend` 来指定不同的后端支持,例如设置为 `'retinaface'`, `'mtcnn'`, 或者其他的选项之一。 #### 自定义显示结果 当成功获取到脸部位置信息后,还可以进一步自定义结果显示方式。下面的例子说明了怎样把检测框画出来并且标注上一些基本信息。 ```python for face in detected_faces: (startX, startY, endX, endY) = int(face['facial_area']['x']), \ int(face['facial_area']['y']), \ int(face['facial_area']['w'] + face['facial_area']['x']), \ int(face['facial_area']['h'] + face['facial_area']['y']) # 绘制矩形框标记面部区域 cv2.rectangle(img, (startX, startY), (endX, endY), (0, 255, 0), 2) # 添加标签 label_position = (startX, startY - 10) font_scale = 0.9 color = (255, 0, 0) thickness = 2 text_to_display = f"{len(detected_faces)} faces found!" cv2.putText(img, text_to_display, label_position , cv2.FONT_HERSHEY_SIMPLEX, font_scale, color, thickness, lineType=cv2.LINE_AA) # 展示最终带有标注的结果图 cv2.imshow('Detected Faces', img) cv2.waitKey(0); cv2.destroyAllWindows() ``` 这段代码不仅实现了基础的功能,还增加了图形化的反馈给用户,使得程序运行后的输出更直观易懂[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值