• <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>

            清風竹林

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

            std::string is contiguous (轉)

            原文地址: 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 李現民 閱讀(580) 評論(3)  編輯 收藏 引用 所屬分類: 絕對盜版

            評論

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

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

            # re: std::string is contiguous (轉) 2011-05-27 10:45 李現民

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

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

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

            一日本道伊人久久综合影| 99久久综合国产精品免费| 久久久久免费精品国产| 亚洲国产一成久久精品国产成人综合 | 99久久精品国产综合一区| 久久久久免费看成人影片| 久久SE精品一区二区| 一本色道久久88—综合亚洲精品| 亚洲天堂久久久| 久久九九兔免费精品6| 国产成人久久精品一区二区三区| 综合久久给合久久狠狠狠97色| 久久免费国产精品| 中文字幕精品无码久久久久久3D日动漫| 久久久久香蕉视频| 久久精品夜色噜噜亚洲A∨| 久久久久久国产精品无码下载| 国产成人久久777777| 国产午夜精品久久久久九九| 精品无码久久久久久久动漫| 久久久久国产| 国产99久久久国产精品小说| 中文字幕乱码人妻无码久久| 欧美黑人又粗又大久久久| 777米奇久久最新地址| 999久久久免费国产精品播放| 国产午夜精品久久久久九九电影| 久久最新免费视频| 伊人色综合久久天天人手人婷| 久久精品中文字幕无码绿巨人| 青青国产成人久久91网| 久久影院久久香蕉国产线看观看| 一本久久精品一区二区| 久久99精品国产自在现线小黄鸭| 日韩精品久久久久久| 亚洲国产精品综合久久一线| 久久久久久毛片免费播放| 777久久精品一区二区三区无码| 久久久久久一区国产精品| 亚洲中文字幕久久精品无码喷水| 国产91久久精品一区二区|