久久久久久久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)嗎?)
主站蜘蛛池模板: 视频在线一区二区 | 天天宗合网 | 日韩精品一区二区三区中文在线 | 97在线观视频免费观看 | 国产精品一区二 | 91在线资源| 日韩中文一区二区三区 | 欧美日韩高清免费 | 久久久久久国产精品免费 | 欧美日韩在线不卡 | 亚洲高清三级 | 成人亚洲在线 | 91久久看片 | 亚洲国产精品日本 | 国产欧美精品一区二区 | 亚洲精品久久久一区二区三区 | 亚洲一二三在线观看 | 亚洲国产成人精 | 久久久久国产精品一区二区 | 在线午夜电影 | 人人澡人人爱 | 精品三级在线观看 | 影视一区| 欧美成视频 | 日本超碰| 99精品在线免费观看 | 欧美一区二区三区视频在线观看 | 精品一区二区在线观看 | 欧美一级二级在线观看 | 激情五月婷婷综合 | 国产一区二区三区在线 | 一级毛片视频 | 99国内精品久久久久久久 | 四虎最新地址 | 精品一区二区三区免费视频 | 日韩免费视频一区二区 | 国产女人与拘做受视频 | 精品欧美一区二区在线观看 | 国产精品一区二 | 欧美一级片在线看 | 一区二区三区免费观看 |