1. 程式人生 > 實用技巧 >C++ 相關的string 函式(memcpy、memove、strtok、strchr)

C++ 相關的string 函式(memcpy、memove、strtok、strchr)

一、memcpy和memove

void* memove(void*destin , void* source, size_t count) {

    //進行這樣的判斷是為了解決拷貝重疊的情況
    if (destin > source) {
        //這裡拷貝的時候還可以提高效率
        //因為CPU單次可以拷貝的最大位元組是8個
        //所以完全可以用long* 替代 char*(前提是count>8)

        char* a = (char*)destin;
        char* b = (char*)source;
        
while (count--) { *b++ = *a++; } } else { char* a = (char*)destin + count; char* b = (char*)source + count; while (count--) { *b-- = *a--; } } return destin; }
memove

二、strtok

參考部落格https://www.cnblogs.com/Bob-tong/p/6610806.html

三、strchr

描述

C 庫函式 char *strchr(const char *str, int c) 在引數 str 所指向的字串中搜索第一次出現字元 c(一個無符號字元)的位置。

宣告

下面是 strchr() 函式的宣告。

char *strchr(const char *str, int c)

引數

  • str -- 要被檢索的 C 字串。
  • c -- 在 str 中要搜尋的字元。

返回值

該函式返回在字串 str 中第一次出現字元 c 的位置,如果未找到該字元則返回 NULL。

例項