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

            狠狠色丁香婷婷久久综合| 亚洲国产精品久久久久婷婷老年| 国产一区二区精品久久岳| 久久国产精品一区| 亚洲一级Av无码毛片久久精品| 久久综合亚洲色HEZYO社区| 久久午夜羞羞影院免费观看| 久久天堂电影网| 欧美日韩精品久久免费| 伊人久久综在合线亚洲2019| 亚洲国产成人久久精品99| 久久久久亚洲av无码专区导航 | 午夜精品久久久久久| 亚洲精品无码久久久影院相关影片| 久久综合噜噜激激的五月天| 久久精品国产一区二区三区| 亚洲AV无码久久精品色欲| 狠狠人妻久久久久久综合蜜桃| 亚洲国产欧洲综合997久久| 久久久久99精品成人片三人毛片| 精品久久久久久国产潘金莲| 亚洲精品无码久久毛片| 久久久久夜夜夜精品国产| 天天爽天天狠久久久综合麻豆| 亚洲国产成人精品无码久久久久久综合| 久久精品黄AA片一区二区三区| 亚洲日本va午夜中文字幕久久| 国产精品熟女福利久久AV| 久久亚洲天堂| 久久这里只有精品视频99| 成人精品一区二区久久| 日韩精品国产自在久久现线拍 | 久久99精品国产麻豆宅宅| 人妻精品久久久久中文字幕一冢本 | 久久99精品国产| 久久青青草原国产精品免费| 91精品国产9l久久久久| 亚洲狠狠综合久久| 色综合久久88色综合天天| 久久成人精品视频| 国产91久久综合|