[C++stdlibベース]文字列の操作について——C++標準ライブラリヘッダファイル

4970 ワード

ネット上の例
まとめ
/*     <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
*/

≪インスタンス|Instance|emdw≫
#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
",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
",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
", str_copy); //abc strcpy(str_copy,str1); printf("%s
", 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'.
",buffer1,buffer2);//n=1 else if (m<0) printf ("'%s' is less than '%s'.
",buffer1,buffer2); else printf ("'%s' is the same as '%s'.
",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
",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.
", pch-str+1);//5 ; else printf ("'p' not found.
"); pch=strchr(str,'s'); while (pch!=NULL) { printf ("found at %d
",pch-str+1);//9 pch=strchr(pch+1,'s'); } pch = strstr (str,"st"); printf("%d
",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
",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()); } }