string的c str 和data 有區別?

2021-06-13 00:45:42 字數 640 閱讀 8299

本機windows系統安裝了vs2008環境下,檢視xstring檔案的原始碼發現xstring類的c_str(),data()實現如下

const _elem *__clr_or_this_call c_str() const

const _elem *__clr_or_this_call data() const

這難道有區別嗎???

寫了個測試**如下:

#include#include#includeusing std::string;

int _tmain(int argc, _tchar* argv)

結果是:

stopic.c_str()=23859240, sizeof(...)=4, strlen(...)=11

stopic.data()=23859240, sizeof(...)=4, strlen(...)=11

modify [0]='a' after const_cast(stopic.c_str() ) stopic=aello world

modify [1]='b' after const_cast(stopic.data() ) stopic=abllo world

注意string與c str 使用帶來的問題

c str 函式原型是 const char c str const const elem c str const 作用 是乙個c 函式,用於將string型別的字串轉換成c的普通字串 因為string是c 中才出現的,c中只有char 這樣字串的概念 返回值 返回乙個臨時的指標,由於是臨時的,所以...

c 中string類成員函式c str

1.string類成員函式c str 的原型 const char c str const 返回乙個以null終止的c字串 2.c str 函式返回乙個指向正規c字串的指標,內容和string類的本身物件是一樣的,通過string類的c str 函式能夠把string物件轉換成c中的字串的樣式 3....

java通用日期轉換(string 轉 date)

這是自己工作是寫的乙個工具類,記錄下來,以後要用直接copy,這個工具主要實現的是將任意格式的字串轉換為data格式 當然,首先你的日期必須是個日期才行 主要判定的規則是 年月日時分秒之間的分割均不能包含資料,否則轉化出來的日期 嘿嘿 自己寫的,可能有不足,見諒 將字串轉化為時間物件 param d...