亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
123
最近訪問板塊 發(fā)新帖
樓主: hy036630
打印 上一主題 下一主題

std::string你也許不知道的秘密 [復(fù)制鏈接]

論壇徽章:
2
青銅圣斗士
日期:2015-11-26 06:15:59數(shù)據(jù)庫技術(shù)版塊每日發(fā)帖之星
日期:2016-07-24 06:20:00
21 [報告]
發(fā)表于 2011-12-29 18:45 |只看該作者
c_str() 返回一個數(shù)組, 保證有size()+1個元素, 前size()個元素是字符串內(nèi)容(可能有0), s.c_str()[size()] 是0 。
而data() 返回一個數(shù)組。 如果size()>0 則保證有size()個元素, 前size個元素是字符串內(nèi)容, 沒說它不可以有size()+1個元素啊……

一種實現(xiàn):
capacity()始終>=size()+1。
data() 直接返回內(nèi)部數(shù)組的地址。
c_str() 也返回這個數(shù)組的地址, 但返回之前將末尾填個0……

論壇徽章:
11
未羊
日期:2013-12-16 12:45:4615-16賽季CBA聯(lián)賽之青島
日期:2016-04-11 19:17:4715-16賽季CBA聯(lián)賽之廣夏
日期:2016-04-06 16:34:012015亞冠之卡爾希納薩夫
日期:2015-11-10 10:04:522015亞冠之大阪鋼巴
日期:2015-07-30 18:29:402015亞冠之城南
日期:2015-06-15 17:56:392015亞冠之卡爾希納薩夫
日期:2015-05-15 15:19:272015亞冠之山東魯能
日期:2015-05-14 12:38:13金牛座
日期:2014-12-04 15:34:06子鼠
日期:2014-10-16 13:40:4715-16賽季CBA聯(lián)賽之八一
日期:2016-07-22 09:41:40
22 [報告]
發(fā)表于 2011-12-29 18:53 |只看該作者
OwnWaterloo 發(fā)表于 2011-12-29 18:45
c_str() 返回一個數(shù)組, 保證有size()+1個元素, 前size()個元素是字符串內(nèi)容(可能有0), s.c_str() 是0  ...


好吧, 我承認(rèn)是更好一些, 而且我之前也沒想到這個辦法

論壇徽章:
0
23 [報告]
發(fā)表于 2011-12-30 10:04 |只看該作者
回復(fù) 5# zylthinking


    你見過哪家的 std::string::c_str() 是這么實現(xiàn)的?

論壇徽章:
11
未羊
日期:2013-12-16 12:45:4615-16賽季CBA聯(lián)賽之青島
日期:2016-04-11 19:17:4715-16賽季CBA聯(lián)賽之廣夏
日期:2016-04-06 16:34:012015亞冠之卡爾希納薩夫
日期:2015-11-10 10:04:522015亞冠之大阪鋼巴
日期:2015-07-30 18:29:402015亞冠之城南
日期:2015-06-15 17:56:392015亞冠之卡爾希納薩夫
日期:2015-05-15 15:19:272015亞冠之山東魯能
日期:2015-05-14 12:38:13金牛座
日期:2014-12-04 15:34:06子鼠
日期:2014-10-16 13:40:4715-16賽季CBA聯(lián)賽之八一
日期:2016-07-22 09:41:40
24 [報告]
發(fā)表于 2011-12-30 10:20 |只看該作者
本帖最后由 zylthinking 于 2011-12-30 10:21 編輯
giantchen 發(fā)表于 2011-12-30 10:04
回復(fù) 5# zylthinking


我返回去仔細(xì)想了想, 發(fā)覺是被誤導(dǎo)了:


string::c_str
public member function
const char* c_str ( ) const;
Get C string equivalent

Generates a null-terminated sequence of characters (c-string) with the same content as the string object and returns it as a pointer to an array of characters.

A terminating null character is automatically appended.

The returned array points to an internal location with the required storage space for this sequence of characters plus its terminating null-character, but the values in this array should not be modified in the program and are only guaranteed to remain unchanged until the next call to a non-constant member function of the string object.


string::data
public member function
const char* data() const;
Get string data

Returns a pointer to an array of characters with the same content as the string.

Notice that no terminating null character is appended (see member c_str for such a functionality).

The returned array points to an internal location which should not be modified directly in the program. Its contents are guaranteed to remain unchanged only until the next call to a non-constant member function of the string object.

看到以上時, 腦筋沒轉(zhuǎn)彎; 事后又忘記的差不多, 只記得有這么回事, 誤以為是從源碼看到的

論壇徽章:
0
25 [報告]
發(fā)表于 2011-12-30 14:40 |只看該作者
oh MGD
it is supposed that c_str() return the pointer of the array, which should not malloc a copy.

論壇徽章:
0
26 [報告]
發(fā)表于 2011-12-31 20:27 |只看該作者
C使用char[]保存字符串,以結(jié)束符表示字符串結(jié)尾。std::string加了數(shù)據(jù)緩沖區(qū)起始標(biāo)志。
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP