久久久久久久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()的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

這篇文章的評論部分有一個(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中寫入元素,正確的做法是調(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í)上,以下測試代碼在 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;
}

我是對的,還是錯(cuò)的?VS2010 SP1 是對的,還是錯(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)的對象填充任何空間.如果它們是整數(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 來寫入元素 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ì)工作,但是之前存儲的 &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()的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(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?(如何將枚舉類型變量轉(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)嗎?)
主站蜘蛛池模板: 香蕉婷婷 | 亚洲成人免费在线观看 | 日韩精品视频一区二区三区 | 国产男女猛烈无遮掩视频免费网站 | 美女露尿口视频 | 午夜免费观看网站 | 久久一及片 | 给我免费的视频在线观看 | 欧美精品首页 | 日韩免费高清视频 | 亚洲第1页 | 午夜精品| 欧美一区二区三区四区视频 | 国产精品亚洲精品 | 在线观看亚洲一区二区 | 日韩和的一区二区 | 国产伦精品一区二区三区照片91 | 午夜视频精品 | 久久99这里只有精品 | 偷拍亚洲色图 | 羞羞的视频在线看 | 欧美一级二级三级视频 | 91影库 | 欧美三级在线 | 午夜a区 | 亚洲日日夜夜 | 亚洲a视频 | 91中文字幕在线观看 | 日本韩国电影免费观看 | 97久久精品午夜一区二区 | 日韩久久综合 | aaa一区 | 国产精品久久久久久久久久三级 | 亚洲国产精品久久 | 久久人人爽人人爽人人片av免费 | 草比网站 | 午夜电影网 | 精品久久中文字幕 | 美女国内精品自产拍在线播放 | av天天干| 日本久久综合网 |