• <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) 評(píng)論(3)  編輯 收藏 引用 所屬分類: 絕對(duì)盜版

            評(píng)論

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

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

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

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

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

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

            久久无码av三级| 亚洲第一永久AV网站久久精品男人的天堂AV | 国产69精品久久久久久人妻精品| 日日狠狠久久偷偷色综合免费| 久久久久久久久久久久久久 | 亚洲人成无码久久电影网站| 精产国品久久一二三产区区别| 亚洲αv久久久噜噜噜噜噜| 国产精品久久久久影院色| 欧美精品丝袜久久久中文字幕| 久久久无码精品亚洲日韩京东传媒 | 久久亚洲精品人成综合网| 久久久国产精品福利免费 | 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 久久精品蜜芽亚洲国产AV| 国产午夜精品久久久久九九| 中文字幕人妻色偷偷久久 | 亚洲一级Av无码毛片久久精品| 狠狠狠色丁香婷婷综合久久五月| 久久国产欧美日韩精品| 99久久无码一区人妻| 人妻少妇久久中文字幕一区二区 | 精品久久久无码人妻中文字幕豆芽 | 久久性精品| 999久久久国产精品| 99久久精品费精品国产一区二区| 性高朝久久久久久久久久| 久久久久久一区国产精品| 国产精品毛片久久久久久久| 99久久夜色精品国产网站| 久久综合九色综合网站| 久久久久亚洲AV无码去区首| 99热热久久这里只有精品68| 久久久91精品国产一区二区三区| 久久不射电影网| 国产精品久久久久jk制服| 久久99国产综合精品| 99精品国产在热久久无毒不卡 | 人人狠狠综合久久亚洲高清| 日韩久久久久中文字幕人妻| 久久精品成人影院|