cstring常用函数

cstring笔记
1.puts输出字符数组串,char*s,char s[];注意初始化时要尽可能大的空间
2.
复制:
 memcpy ( &person_copy, &person, sizeof(person) );
 printf ("person_copy: %s, %d \n", person_copy.name, person_copy.age );
 
 memcpy注意只能用于char s[]声明的字数数组中,string s 或者 char* s 会报错
 eg:
  char str[] = "hello";
  char temp[10];
  memcpy(temp, str, strlen(str));
  注意,第三个参数输入的是复制str的前几位字符
 memmove 可在内存重叠的情况下使用,即可对自身字符串进行复制。也是只针对char s[]才能运行
 strcpy     char * strcpy ( char * destination, const char * source );
                   注意,strcpy 仍然只能用于char s[]
 strncpy 可以赋值确定的长度到另一个字符数组中去。多了一个n,即num_size。
链接:
 strcat(char*dest,const char* source)将source连接在dest后面。
 strncat(char *dest, const char* source, size_t num)将前num个source元素添加到dest尾部。
比较:
 int memcmp(const void * ptr1, const void * ptr2, size_t num);//return "int",const!!!num is  前num个比较
  =0 完全一样
  -1 ptr1 < ptr2
  +1 ptr1 > ptr2
 int strcmp(const char* s1,const char* s2)
  同memcmp
寻找: memchr:
  const void * memchr ( const void * ptr, int value, size_t num );
             void * memchr (       void * ptr, int value, size_t num );
  在ptr中寻找值为value的第一个元素,并将指针指向这个元素。比如:communication中寻找m,找到后,p指向m  所在的位置,所以,p = mmunication注意,这里返回值类型是一个空指针,所以char *p, p = (char*)memchr   (str,'m',20)。
  要想获得位置:p - str + 1 即为第一个查找到元素的位置。(地址首元素之差)

 strchr:
  const char * strchr ( const char * str, int character );
             char * strchr (       char * str, int character );
  Returns a pointer to the first occurrence of character in the C string str.
  比memchar少了size_t num,并且,指针不用进行强制格式转换,其余均与menchar相同。
 strcspn:
  size_t strcspn ( const char * str1, const char * str2 );
  s1 = communication
  s2 = banana
  strcspn(s1,s2) = 5  //  s2的n在s1中第一个找到,位置为5
 strspn:
  size_t strspn ( const char * str1, const char * str2 );
  Get span of character set in string
  Returns the length of the initial portion of str1 which consists only of characters that are    part of str2.
  The search does not include the terminating null-characters of either strings, but ends there.
  eg:
  char strtext[] = "129th";
  char cset[] = "1234567890";
  i = strspn (strtext,cset);
   printf ("The initial number has %d digits.\n",i);
  result:
  The initial number has 3 digits.

 strrchr:
  const char * strrchr ( const char * str, int character );
             char * strrchr (       char * str, int character );
  eg:
  char str[] = "This is a sample string";
    char * pch;
   pch=strrchr(str,'s');
    printf ("Last occurence of 's' found at %d \n",pch-str+1);
  result:
  Last occurrence of 's' found at 18
 strstr:
  const char * strstr ( const char * str1, const char * str2 );
             char * strstr (       char * str1, const char * str2 );
  Locate substring
  Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not   part of str1.
  The matching process does not include the terminating null-characters, but it stops there.
    eg:
  char str[] ="This is a simple string";
    char * pch;
   pch = strstr (str,"simple");
   strncpy (pch,"sample",6);
    puts (str);
  result:
  This is a sample string
  
替换:
 memset:
  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).
  char str[] = "almost every programmer should know memset!";
    memset (str,'-',6);
   puts (str);
  ------ every programmer should know memset!

3.string s 的复制可以直接利用“=”进行赋值
  string s = "hello";
  string temp = s;
  cout <<temp << endl; // Now,temp is "hello"!


猜你喜欢

转载自blog.csdn.net/newandbetter/article/details/80286271