• <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 李現民 閱讀(590) 評論(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屬性,一樣可以用。  回復  更多評論   

            国产成年无码久久久免费| 一本一本久久a久久精品综合麻豆| 亚洲国产美女精品久久久久∴| 亚洲国产精品无码久久SM| www性久久久com| 武侠古典久久婷婷狼人伊人| 国产成人精品免费久久久久| 久久久黄片| 久久天堂电影网| 久久狠狠爱亚洲综合影院| 国产成人久久777777| 久久精品无码一区二区无码 | 国产精品99久久99久久久| 久久强奷乱码老熟女网站| 久久久无码一区二区三区| 一级做a爰片久久毛片毛片| 久久噜噜电影你懂的| 亚洲精品乱码久久久久久自慰| 久久91这里精品国产2020| 国产亚洲欧美精品久久久| 色综合久久夜色精品国产| 一本伊大人香蕉久久网手机| 国产人久久人人人人爽| 波多野结衣AV无码久久一区| 人人狠狠综合久久亚洲| 国产伊人久久| 国产精品成人99久久久久91gav| 国内精品久久久久久99蜜桃| 久久久SS麻豆欧美国产日韩| 国产精品久久久久久久app| 久久婷婷五月综合色99啪ak| 久久精品无码免费不卡| 国产高清国内精品福利99久久| 久久成人国产精品| 精品久久久久久无码专区 | 国产成人久久精品麻豆一区| 99久久超碰中文字幕伊人| 久久午夜无码鲁丝片| 精品国产一区二区三区久久久狼| 久久精品中文字幕无码绿巨人 | 国产精品久久久久久影院 |