stl原始碼簡析char traits h

2021-07-29 15:47:18 字數 1829 閱讀 8000

掠過之前不重要的東西,直接上**了

static

void

assign(char_type& __c1, const char_type& __c2)

static

bool eq(const _chart& __c1, const _chart& __c2)

static

bool lt(const _chart& __c1, const _chart& __c2)

這 三個函式很簡單

第乙個是字元賦值

第二個比較是否相等

第三個比較是否小於

我們繼續往下看

static int compare(const _chart* __s1, const _chart* __s2, size_t __n)
這就是比較字串是否相等的函式,相等返回0,大於返回1,小於返回-1

怎麼樣是不是很簡單,自己都可以寫出來

再看一下length函式

static size_t length(const _chart* __s) 

return __i;

}

這裡注意了,返回長度的時候並不是你字串長度為多少就給你返回多少,比如

char a[5]="abcdefgh";
當你呼叫length函式的時候是從a[0]的位置開始一直找到『\0』才結束

所以length(a)=8而並不是我們想象中的5

static const _chart* find(const _chart* __s, size_t __n, const _chart& __c)

查詢特定字元的函式,也很簡單遍歷一次,這裡就不再贅述

繼續往下

static _chart* move(_chart* __s1, const _chart* __s2, size_t __n) 

static _chart* copy(_chart* __s1, const _chart* __s2, size_t __n)

static _chart* assign(_chart* __s, size_t __n, _chart __c)

看函式名就可以知道它們的具體功能,這裡也沒什麼需要注意的地方,也不多講了

static char_type to_char_type(const int_type& __c) 

static int_type to_int_type(const char_type& __c)

函式功能參考函式名,這裡的轉換就是這麼簡單粗暴

最後一部分

static

int compare(const

char* __s1, const

char* __s2, size_t __n)

static size_t length(const

char* __s)

static

void assign(char& __c1, const

char& __c2)

static

char* assign(char* __s, size_t __n, char __c)

在另乙個模板類裡面又有乙個compare函式 不過實現方式不太一樣,這個是直接呼叫memcmp函式

到這裡這部分的原始碼就看完了,怎麼樣,其實也還是很簡單的。大家可以多看看原始碼,不僅可以學到很多新姿勢,對自己的幫助也很大

Sample BSP原始碼簡析

ifndef bsp h define bsp h include sdksample.h include filesystemlayer.h filesystemlayer.h 用來處理檔案系統的目錄 路徑等資訊 後面的mfslayer getconfigfilepath就是用了該檔案中定義的類。...

libc hashtable 原始碼簡析

本文分析的是 中截止至 2016 年 1 月 30 日最新的libc libc 中,hashtable的實現為鏈式結構。在教科書 introduction to algorithm 3rd edition 中,介紹的實現是由乙個陣列作為buckets,每個陣列中儲存乙個鍊錶。但是libc 中,使用乙...

HashMap原始碼簡析

hashmap 基於map介面實現的,允許使用null值和null鍵,但資料無序的.劃重點 執行緒不安全.若是想獲取乙個執行緒安全的hashmap,可用下面方法 map map collections.synchronizedmap new hashmap hashmap的主幹是entry陣列,每乙...