久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

std::vector::resize() 與 std::vector::reserve()

std::vector::resize() vs. std::vector::reserve()(std::vector::resize() 與 std::vector::reserve())
本文介紹了std::vector::resize() 與 std::vector::reserve()的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

這篇文章的評(píng)論部分有一個(gè)關(guān)于使用 std::vector::reserve 的帖子() vs. std::vector::resize().

There is a thread in the comments section in this post about using std::vector::reserve() vs. std::vector::resize().

這是原始代碼:

void MyClass::my_method()
{
    my_member.reserve(n_dim);
    for(int k = 0 ; k < n_dim ; k++ )
         my_member[k] = k ;
}

我認(rèn)為要在vector中寫(xiě)入元素,正確的做法是調(diào)用std::vector::resize(),不是 std::vector::reserve().

I believe that to write elements in the vector, the correct thing to do is to call std::vector::resize(), not std::vector::reserve().

事實(shí)上,以下測(cè)試代碼在 VS2010 SP1 的調(diào)試版本中崩潰":

In fact, the following test code "crashes" in debug builds in VS2010 SP1:

#include <vector>

using namespace std;

int main()
{
    vector<int> v;
    v.reserve(10);
    v[5] = 2;

    return 0;
}

我是對(duì)的,還是錯(cuò)的?VS2010 SP1 是對(duì)的,還是錯(cuò)的?

Am I right, or am I wrong? And is VS2010 SP1 right, or is it wrong?

推薦答案

有兩種不同的方法是有原因的:

There are two different methods for a reason:

std::vector::reserve 將分配內(nèi)存但不會(huì)調(diào)整向量的大小,向量的邏輯大小與之前相同.

std::vector::reserve will allocate the memory but will not resize your vector, which will have a logical size the same as it was before.

std::vector::resize 實(shí)際上會(huì)修改向量的大小,并會(huì)用處于默認(rèn)狀態(tài)的對(duì)象填充任何空間.如果它們是整數(shù),則它們都為零.

std::vector::resize will actually modify the size of your vector and will fill any space with objects in their default state. If they are ints, they will all be zero.

reserve 之后,在你的情況下,你需要很多 push_backs 來(lái)寫(xiě)入元素 5.如果您不想這樣做,那么在您的情況下,您應(yīng)該使用調(diào)整大小.

After reserve, in your case, you will need a lot of push_backs to write to element 5. If you don't wish to do that then in your case you should use resize.

關(guān)于保留的一件事:如果您隨后使用 push_back 添加元素,直到達(dá)到您保留的容量,任何現(xiàn)有的引用、迭代器或指向向量中數(shù)據(jù)的指針都將保持有效.所以如果我保留 1000 并且我的大小是 5,&vec[4] 將保持不變,直到向量有 1000 個(gè)元素.之后,我可以調(diào)用 push_back() 并且它會(huì)工作,但是之前存儲(chǔ)的 &vec[4] 指針可能不再有效.

One thing about reserve: if you then add elements with push_back, until you reach the capacity you have reserved, any existing references, iterators or pointers to data in your vector will remain valid. So if I reserve 1000 and my size is 5, the &vec[4] will remain the same until the vector has 1000 elements. After that, I can call push_back() and it will work, but the stored pointer of &vec[4] earlier may no longer be valid.

這篇關(guān)于std::vector::resize() 與 std::vector::reserve()的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both(將 RGB 轉(zhuǎn)換為 HSV 并將 HSV 轉(zhuǎn)換為 RGB 的算法,范圍為 0-255)
How to convert an enum type variable to a string?(如何將枚舉類(lèi)型變量轉(zhuǎn)換為字符串?)
When to use inline function and when not to use it?(什么時(shí)候使用內(nèi)聯(lián)函數(shù),什么時(shí)候不使用?)
Examples of good gotos in C or C++(C 或 C++ 中好的 goto 示例)
Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);(ios_base::sync_with_stdio(false) 的意義;cin.tie(NULL);)
Is TCHAR still relevant?(TCHAR 仍然相關(guān)嗎?)
主站蜘蛛池模板: 中文字幕中文字幕 | 日韩精品视频在线播放 | av网站在线免费观看 | 亚洲精品一区二三区不卡 | 亚洲欧美一区二区三区在线 | 久久精品欧美 | 丨国产丨调教丨91丨 | 国产三级在线观看视频 | 国产精品999 | 国产一级免费 | av福利网| 欧美国产一区二区 | 婷婷久久综合 | av大片在线观看 | 久久最新视频 | 麻豆一级片 | 久久精品视频一区二区 | 成av人片在线观看www | 国产又色又爽又黄又免费 | 国产伦精品一区二区三区88av | 少妇中文字幕 | 欧美精品第一页 | 日韩一级免费视频 | 日韩精品在线一区二区 | 精品一区二区国产 | 色哟哟入口国产精品 | 久久麻豆视频 | 精品免费视频 | 亚洲二区在线 | 日日摸天天添天天添破 | 一区二区三区国产精品 | av一级在线| 欧美日韩一二区 | 国产精品成人在线观看 | 超碰精品在线 | 黑人精品一区二区 | 91在线成人| 免费看av的网址 | 人人看av| 天天干天天色 | 日本高清视频www |