<cstring>相关方法的使用

void* memcpy(void* destination, const void* source, size_t num);
/*
参数: 	destination - 指向要复制内容的目标数组的指针,类型转换为void *类型的指针。
	  	source	    - 指向要复制的数据源的指针,类型转换为const void *类型的指针。
	  	num		    - 要复制的字节数,size_t是无符号整数类型。
返回: 	返回destination
ATTENTION:完完全全准确地复制num个字符,不会因为中间有null-character而终止。
 */

void* memmove(void* destination, const void* source, size_t num);
/*
参数: 	destination - 指向要复制内容的目标数组的指针,类型转换为void *类型的指针。
	   	source	    - 指向要复制的数据源的指针,类型转换为const void *类型的指针。
	  	num		    - 要复制的字节数,size_t是无符号整数类型。
返回: 	返回destination
ATTENTION:复制有缓冲区,所以允许destination和source重叠
 */

char* strncpy(char* destination, const char* source, size_t num);
/*
参数: 	destination - 指向要复制内容的目标数组的指针。
	  	source	    - 要复制的C字符串。
	  	num		    - 从source复制的最大字符数,size_t是无符号整数类型。
返回: 	返回destination
ATTENTION:1.如果source的结束位置(C string,以null-character结束)被找到的时候,\
		   往destination中复制的字符数还未到num个,即source短于num,那么会往destination中赋0直到达到num;
		   2.如果source的长度大于num,那么复制到destination的结尾并不会加上null-character,\
		   即destination不应该看做C string,如果尝试去读取destination的内容的话,会发生溢出。
 */

char* strcpy(char* destination, const char* source);
/*
参数: 	destination - 指向要复制内容的目标数组的指针。
	  	source	    - 要复制的C字符串。
返回: 	返回destination
ATTENTION:1.确保destination的长度大于source的长度(包括结尾的null-character),不然会溢出。
		   2.复制整个source字符串到destination中,包括null-character(停止复制的点)
 */


char* strncat(char* destination, const char* source, size_t num);
/*
参数: 	destination - 指向要目标数组的指针,该数组应该含有C string,并且有足够大的空间来包含附加\
	  	的字符,包括结尾的null-character。
	  	source	    - 要附加到目标数组结尾的C字符串。
	  	num		    - 被附加到destination末尾的最大字符数,size_t是无符号整数类型。
返回: 	返回destination
ATTENTION:1.destination本身应该含有C string,并且有足够大的空间来包含附加的字符,包括结尾的null-character。
 */

int memcmp(const void* ptr1, const void* ptr2, size_t num);
/*
参数: 	ptr1 - 指向字符数组1的指针。
	  	ptr2 - 指向字符数组2的指针。
	  	num  - 要比较的字符数量,size_t是无符号整数类型。
返回: 	0    - 该num个字符相同
	  	>0   - 该num个字符不相同,并且第一对不同的值ptr1更大(以unsigned char值来计算的话)
	  	<0   - 该num个字符不相同,并且第一对不同的值ptr2更大(以unsigned char值来计算的话)
ATTENTION:并不像strcmp,该函数就算找到了null-character也不会停止比较,而是会完全地比较num个字符。
 */

int strncmp_(const char* ptr1, const char* ptr1, size_t num);
/*
参数: 	ptr1 - 指向字符数组1的指针。
	  	ptr2 - 指向字符数组2的指针。
	  	num  - 要比较的字符数量,size_t是无符号整数类型。
返回: 	0    - 该num个字符相同
	  	>0   - 该num个字符不相同,并且第一对不同的值ptr1更大(以unsigned char值来计算的话)
	  	<0   - 该num个字符不相同,并且第一对不同的值ptr2更大(以unsigned char值来计算的话)
ATTENTION:比较停止的标志有三种:1.找到了不同的字符 2.找到了null-character 3.已经比较了num个字符
 */

//const void * memchr ( const void * beSerched, int value, size_t num ); C++中,该函数被重载,C中只有下面这个声明
void * memchr(void * beSerched, int value, size_t num);
/*
参数: 	beSerched - 指向要搜索的字符数组的指针
	  	value     - 要定位的值,该值作为int传递,但函数使用该int值转换的unsigned char值一个字节一个字节搜索
	  	num       - 被搜索的字符数量,size_t是无符号整数类型。
返回: 	指向要定位的值首次出现的内存地址的指针,如果没有找到要定位的值,则返回空指针。
ATTENTION:有可能返回空指针。
 */

// const char* strrchr(const char* beScaned, int character); C++, the function is overloaded.
char* strrchr(char* beScaned, int character);
/*
参数: 	beScaned  - C string to be scanned.
	  	character - Character to be located. It is passed as its int promotion, but it is internally converted back to char.
返回: 	A pointer to the last occurrence of character in beScaned.
	  	If the character is not found, the function returns a null pointer.
ATTENTION:1.The terminating null-character is considered part of the C string. 
			Therefore, it can also be located to retrieve a pointer to the end of a string.
		   2.It will return nullptr in some sutiations.
 */

size_t strcspn(const char* beScaned, const char* source);
/*
参数: 	beScaned - 指向要被扫描的字符数组的指针
	  	source   - 指向包含着要匹配的值的字符数组的指针
返回: 	在beScaned中找到第一个匹配的值之前扫描的字符数量,如果beScaned中不包含任何一个source的字符,那么会返回beScaned的长度。
ATTENTION:1.扫描匹配包括null-character。
		   2.是通过unsigned char的值来进行匹配的,如果相比较中文字符的话,慎用。
 */

size_t strspn(const char* beScaned, const char* beMatched);
/*
参数: 	beScaned  - C string to be scanned.
	  	beMatched - C string containing the characters to match.
返回: 	The length of the initial portion of beScaned containing only characters that appear in beMatched.
		Therefore, if all of the characters in beScaned are in beMatched, the function returns the length of the entire beScaned string, 
		and if the first character in beScaned is not in beMatched, the function returns zero.
		size_t is an unsigned integral type.
ATTENTION:1.if the first character in beScaned is not in beMatched, the function returns zero.
*/

// const char* strpbrk (const char* beScaned, const char* beMatched); C++, the function is overloaded.
char* strpbrk(char* beScaned, const char* beMatched);
/*
参数: 	beScaned - C string to be scanned.
	  	beMatched   - C string containing the characters to match.
返回: 	A pointer to the first occurrence in beScaned of any of the characters that are part of beMatched, 
	  	or a null pointer if none of the characters of beMatched is found in beScaned before the terminating null-character.
	  	If none of the characters of beMatched is present in beScaned, a null pointer is returned.
ATTENTION:1.The search does not include the terminating null-characters of either strings, but ends there.
		   2.It will return nullptr in some sutiations.
 */


// const char* strstr(const char* beScaned, const char* beMatched); two overloaded versions provided in C++.
char* strstr(char* beScaned, const char* beMatched);
/*
参数: 	beScaned  - C string to be scanned.
	  	beMatched - C string containing the sequence of characters to match.
返回: 	A pointer to the first occurrence in beScaned of the entire sequence of characters specified in beMatched, 
		or a null pointer if the sequence is not present in beScaned.
ATTENTION:It will return nullptr in some sutiations.
 */

char* strtok(char* source, const char* delimiters);
/*
参数: 	source     - C string to truncate.
		Notice that this string is modified by being broken into smaller strings (tokens).
		Alternativelly, a null pointer may be specified, in which case the function continues scanning where a previous
		successful call to the function ended.
	  	delimiters - C string containing the delimiter characters.
	  	These can be different from one call to another.
返回: 	If a token is found, a pointer to the beginning of the token.
		Otherwise, a null pointer.
		A null pointer is always returned when the end of the C string is reached in the string being scanned.
ATTENTION:1.On a first call, the function expects a C string as argument for source, 
			whose first character is used as the starting location to scan for tokens. 
			In subsequent calls, the function expects a null pointer,
			and uses the position right after the end of the last token as the new starting location for scanning.
		   2.To determine the beginning and the end of a token, 
			the function first scans from the starting location for the first character not contained in delimiters,
			and then scans starting from this beginning of the token for the first character contained in delimiters, 
			which becomes the end of the token.
		   3.The scan also stops if the terminating null character is found.
		   4.This end of the token is automatically replaced by a null-character.
		   5.Once the terminating null character of source is found in a call to strtok,
		    all subsequent calls to this function (with a null pointer as the first argument) return a null pointer.
		   6.NOTICE the source string is modified by being broken into smaller strings.
 */

自己标记一些有意思的方法用以提醒吧:

  • strncpy中,如果source字符串的长度大于要拷贝的长度,那么拷贝到destination字符串中,最后不会自动加上null-character,如果没有后续的操作,那么读取destination字符串会发生溢出。
  • 很多返回指针的方法都有可能返回空指针。
  • strcspn和strspn方法对于找到字符串中特殊的子串有着很好的使用体验。
  • strstr用于找到子串的位置;strcbrk则可以判断某个字符元素集中的元素是否在字符串中出现。
  • strtok可以根据指定的分隔符元素集来对字符串进行分割。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值