• <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)民 閱讀(579) 評論(3)  編輯 收藏 引用 所屬分類: 絕對盜版

            評論

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

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

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

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

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

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

            色天使久久综合网天天| 97r久久精品国产99国产精| 久久亚洲国产精品123区| 国产毛片久久久久久国产毛片| 国内精品免费久久影院| 久久人人添人人爽添人人片牛牛| 一本一道久久综合狠狠老| 91精品观看91久久久久久| 久久国产精品无| 色综合久久最新中文字幕| 香蕉久久影院| 久久综合九色综合欧美狠狠| 久久人人爽人爽人人爽av| 国产情侣久久久久aⅴ免费| 久久本道久久综合伊人| 国内精品久久久久久久97牛牛| 久久久久人妻一区精品| 久久91亚洲人成电影网站| 日韩人妻无码一区二区三区久久99 | AAA级久久久精品无码片| 久久夜色撩人精品国产| 国产精品久久久久久影院| 午夜精品久久久久久影视riav| 久久久青草青青亚洲国产免观| 久久久久99这里有精品10| 久久久久久无码国产精品中文字幕| 久久精品人成免费| 亚洲国产欧洲综合997久久| 日韩美女18网站久久精品| 国内精品久久久久国产盗摄| 久久久无码一区二区三区| 波多野结衣AV无码久久一区| 香蕉久久久久久狠狠色| 亚洲欧洲久久久精品| 久久婷婷五月综合色99啪ak| 精品国产乱码久久久久久浪潮| 亚洲欧美日韩精品久久| 久久亚洲国产精品一区二区| 久久久精品免费国产四虎| 久久婷婷综合中文字幕| 国产精品嫩草影院久久|