[C++STDlib基础]关于字符串的操作——C++标准库头文件

网上实例

总结

/*  头文件<cstring>
_STD_BEGIN
using _CSTD size_t; using _CSTD memchr; using _CSTD memcmp;
using _CSTD memcpy; using _CSTD memmove; using _CSTD memset;
using _CSTD strcat; using _CSTD strchr; using _CSTD strcmp;
using _CSTD strcoll; using _CSTD strcpy; using _CSTD strcspn;
using _CSTD strerror; using _CSTD strlen; using _CSTD strncat;
using _CSTD strncmp; using _CSTD strncpy; using _CSTD strpbrk;
using _CSTD strrchr; using _CSTD strspn; using _CSTD strstr;
using _CSTD strtok; using _CSTD strxfrm;
_STD_END 共22个函数
拷贝函数(4个):memcpy,strcpy,strncpy,memmove
连接函数(2个):strcat,strncat
比较函数(5个):memcmp,strcmp,strncmp,strxfrm,strcoll
搜索函数(8个):memchr,strchr,strcspn,strpbrk,strrchr,strspn,strstr,strtok
其他函数(3个):memset,strerror,strlen
*/

实例

#include <conio.h>
#include <cstring>
#include <iostream>
using namespace std;
/*拷贝函数
void * memcpy ( void * destination, const void * source, size_t num );//可以复制字符串,结构体等
void * memmove ( void * destination, const void * source, size_t num );
char *strncpy(char *destin, char *source, int maxlen); 
*/
void test0()
{
	struct 
	{
		char name[40];
		int age;
	} person, person_copy;
	char myname[]="Pierre";
	printf("sizeof(myname)=%d,strlen(myname)=%d\n",sizeof(myname),strlen(myname));//7(有结束符) 6
	memcpy(person.name,myname,strlen(myname)+1);//strlen(x)只会计算开始到\0之间的字符个数;若不加1,则不会复制结束符,则最后的结果不确定
	person.age=54;
	memcpy(&person_copy,&person,sizeof(person));
	printf("person_copy:%s,%d\n",person_copy.name,person_copy.age);//Pierre  54

	char str_copy[15]; 
	char *str1 = "abcdefghi"; 
	strncpy(str_copy, str1, 3); 
	str_copy[3] = '\0'; 
	printf("%s\n", str_copy); //abc 
	strcpy(str_copy,str1); 
	printf("%s\n", str_copy); //abcdefghi

	/*复制字符串
	1.strcpy的实现
	2.while(*p++ = *s++);
	直接复制或比较字符串字符会失败
	char str1[10], str2[10];
	str1 = “abc”;//Error
	str2 = str1;//Error
	char str3[10] = “abc”;//Ok
	#include <string.h>	strcpy(超出数组长度后的结果无法预料);strncpy;strcat;strlen;strcmp
	*/
}
/*连接函数
char * strcat ( char * destination, const char * source );
char * strncat ( char * destination, const char * source, size_t num );
*/
void test1()
{
	char str1[20];
	char str2[20];
	strcpy (str1,"To be ");
	strcpy (str2,"or not to be");
	strncat (str1, str2, 6);
	puts (str1);//To be or not
}
/*比较函数
int memcmp ( const void * ptr1, const void * ptr2, size_t num );
int strcmp ( const char * str1, const char * str2 );
int strncmp ( const char * str1, const char * str2, size_t num );
*/
void test2()
{
	char buffer1[] = "DWgaOtP12df0";
	char buffer2[] = "DWGAOTP12DF0";
	int m=memcmp ( buffer1, buffer2, sizeof(buffer1) );
	if (m>0) printf ("'%s' is greater than '%s'.\n",buffer1,buffer2);//n=1
	else if (m<0) printf ("'%s' is less than '%s'.\n",buffer1,buffer2);
	else printf ("'%s' is the same as '%s'.\n",buffer1,buffer2);
	 
	char str[][5] = { "R2D2" , "C3PO" , "R2A6" }; 
	for (int n=0 ; n<3 ; n++)
	{
		if (strncmp (str[n],"R2xx",2) == 0)
		{
			printf ("found %s\n",str[n]);//R2D2 R2A6
		}
	}
}
/*搜索函数
void * memchr (void * ptr, int value, size_t num );//查找字符
char * strchr (char * str, int character );//查找字符
char * strstr (char * str1, const char * str2 );//查找字符串
*/
void test3()
{ 
	char str[] = "Example string";
	char* pch = (char*) memchr (str, 'p', strlen(str));//返回找到的地址
	if (pch!=NULL)
		printf ("'p' found at position %d.\n", pch-str+1);//5  由于字符串是连续的;所以他们的地址可以相减
	else
		printf ("'p' not found.\n");
	pch=strchr(str,'s');
	while (pch!=NULL)
	{
		printf ("found at %d\n",pch-str+1);//9
		pch=strchr(pch+1,'s');
	} 
	pch = strstr (str,"st"); 
	printf("%d\n",pch-str+1);//9 
}
/*其他函数 
void *memset(void *s, char ch, unsigned n); 设置s中的所有字节为ch, s数组的大小由n给定 
char * strerror ( int errnum );
*/
void test4()
{
	char str[] = "almost every programmer should know memset!";
	memset (str,'-',6);
	puts (str);//------ every programmer should know memset!	 

	FILE * pFile;
	pFile = fopen ("unexist.ent","r");
	if (pFile == NULL)
		printf ("Error opening file unexist.ent: %s\n",strerror(errno));//: No such file or directory
	/*搜索字符串尾
	1.strlen的实现
	2.while(*s) s++;
	3.while(*s++);
	*/
} 
void Test(char h)
{
	cout<<"press key===="<<h<<endl;
	switch(h)
	{ 
	case '0':  test0();break;
	case '1':  test1();break;
	case '2':  test2();break;
	case '3':  test3();break; 
	case '4':  test4();break;  
	case 27:
	case 'q':exit(0);break; 
	default:cout<<"default "<<h<<endl;break;
	}
}
void main()
{  
	while(1)
	{
		Test(getch());
	} 
} 

你可能感兴趣的:([C++STDlib基础]关于字符串的操作——C++标准库头文件)