• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            清風(fēng)竹林

            ぷ雪飄絳梅映殘紅
               ぷ花舞霜飛映蒼松
                 ----- Do more,suffer less

            std::string is contiguous (轉(zhuǎn))

            原文地址: http://hpyblg.wordpress.com/2010/06/03/stdstring-is-contiguous/

            You can safely assume that the memory buffer used by std::string is contiguous. Specifically, the address of the string’s first character can be used as the address for the whole string, just like a C-style char array:

            1std::string str = "foo";
            2strncpy(&str[0], "bar", 3); // str now contains "bar".

            Why is this safe? The current C++ standard apparently doesn’t guarantee that the string is stored contiguously, but it is in all known implementations. Additionally, the next C++ standard (C++0x) will make this guarantee. So the above usage is valid on all present and future C++ implementations.

            Why is this important? It’s common for functions, especially in the Windows API, to “return” strings by copying them into a buffer passed to the function. Since the memory buffer used in std::string is contiguous you can safely pass it to the function, after resizing the string to the correct size.

            A typical usage for Windows API functions:

            01// get required buffer size
            02DWORD bufSize = 0;
            03GetComputerNameA(NULL, &bufSize);
            04if (!bufSize && GetLastError() != ERROR_BUFFER_OVERFLOW) {
            05  throw std::runtime_error("GetComputerNameA failed");
            06}
            07// bufSize now contains required size of buffer, including null terminator
            08std::string buf(bufSize, '\0');
            09if (!GetComputerNameA(&buf[0], &bufSize)) {
            10  throw std::runtime_error("GetComputerNameA failed");
            11}
            12// bufSize now contains actual size of data
            13buf.resize(bufSize);
            14// now use buf as a regular std::string

            This is cumbersome but actually easier than plain C code, since you don’t have to manage the memory yourself.

            Note that the expression &str[0] is valid only if str isn’t empty. Also, everything I’ve said also applies to std::wstring, the wide-character version of std::string.

            References:

            posted on 2011-05-26 20:52 李現(xiàn)民 閱讀(589) 評(píng)論(3)  編輯 收藏 引用 所屬分類: 絕對(duì)盜版

            評(píng)論

            # re: std::string is contiguous (轉(zhuǎn)) 2011-05-27 10:35 oldman

            為什么不用 str.data() 呢?  回復(fù)  更多評(píng)論   

            # re: std::string is contiguous (轉(zhuǎn)) 2011-05-27 10:45 李現(xiàn)民

            @oldman
            &str[0]的數(shù)據(jù)是可以修改的, 而str.data()的數(shù)據(jù)不能修改  回復(fù)  更多評(píng)論   

            # re: std::string is contiguous (轉(zhuǎn)) 2011-05-27 10:50 oldman

            哦,明白你的意思了。我的意思是說(shuō)既然現(xiàn)在大家對(duì)于data的實(shí)現(xiàn)也是直接拿內(nèi)部數(shù)據(jù)返回的,拿出來(lái)去掉const屬性,一樣可以用。  回復(fù)  更多評(píng)論   

            无码超乳爆乳中文字幕久久 | 狠狠色婷婷久久综合频道日韩 | 日本精品久久久久久久久免费| 久久不见久久见免费影院www日本| 久久91精品综合国产首页| 国产精品久久新婚兰兰| 国产成人精品久久二区二区| 国产精品成人99久久久久| 中文字幕无码免费久久| 亚洲国产精品久久久久| 久久99热这里只有精品66| 国产精品一久久香蕉产线看| 色播久久人人爽人人爽人人片aV| 久久久无码一区二区三区| 国产精品成人无码久久久久久 | 无码人妻久久一区二区三区蜜桃 | 久久精品?ⅴ无码中文字幕| 久久人人爽人人爽人人片av麻烦| 99久久精品毛片免费播放| 青青草原综合久久大伊人| 99久久国产综合精品网成人影院| 亚洲人成精品久久久久 | 热re99久久精品国99热| 午夜肉伦伦影院久久精品免费看国产一区二区三区| 伊人久久大香线蕉综合5g| 久久九九亚洲精品| 蜜臀久久99精品久久久久久小说 | 亚洲精品乱码久久久久66| 久久人人爽人人爽人人片AV东京热| 77777亚洲午夜久久多喷| 青草国产精品久久久久久 | 国产成人香蕉久久久久| 97精品伊人久久大香线蕉app| 亚洲人成伊人成综合网久久久| 久久天天婷婷五月俺也去| 亚洲а∨天堂久久精品| 久久夜色精品国产亚洲av| 久久精品中文字幕有码| 免费一级欧美大片久久网| 久久精品成人影院| 午夜精品久久久久久|