• <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) 評論(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的實(shí)現(xiàn)也是直接拿內(nèi)部數(shù)據(jù)返回的,拿出來去掉const屬性,一樣可以用。  回復(fù)  更多評論   

            亚洲欧美精品一区久久中文字幕| 久久亚洲精品无码aⅴ大香| 久久国产亚洲精品麻豆| 国产精品久久久久久久午夜片 | 亚洲婷婷国产精品电影人久久| 人妻精品久久久久中文字幕| 久久久久久国产精品无码下载 | 久久国产免费直播| 精品国产一区二区三区久久| 久久久精品人妻无码专区不卡| 欧美久久久久久| 国产精品日韩深夜福利久久| 久久亚洲AV成人无码| 久久精品国产精品青草app| 国内精品伊人久久久影院| 国产情侣久久久久aⅴ免费| 日韩精品无码久久一区二区三| 久久久久亚洲AV无码网站| 一个色综合久久| 久久精品国产WWW456C0M| 久久精品国产亚洲av高清漫画| 一97日本道伊人久久综合影院| 久久91综合国产91久久精品| 狠狠色噜噜色狠狠狠综合久久| 91久久精品视频| 狠狠色丁香久久婷婷综| 国产A级毛片久久久精品毛片| 日本精品久久久久影院日本| 久久99精品国产99久久6男男| 亚洲国产另类久久久精品小说| 色综合合久久天天给综看| 99精品久久久久久久婷婷| 久久96国产精品久久久| 精品久久一区二区三区| 2021精品国产综合久久| 久久久久国产精品熟女影院 | 丁香色欲久久久久久综合网| 一本大道久久香蕉成人网| 亚洲人成网站999久久久综合| 人妻无码久久精品| 精品多毛少妇人妻AV免费久久|