簡體   English   中英

C ++:std :: vector :: resize與“normal”分配

[英]C++: std::vector::resize vs. “normal” allocation

std::transform的代碼示例中,有一個代碼如下:

std::vector<int> foo;
std::vector<int> bar;

//add some elements to foo

bar.resize(foo.size());

//store elements transformed from foo's in bar

我想知道是否

std::vector<int> bar;    
bar.resize(foo.size());

有什么不同

std::vector<int> bar(foo.size());

如果是的話,怎么樣?

沒有什么區別,但后者是一點點更高效和簡潔。

不,沒有區別。 至少不是你顯示它的方式(在bar的定義和resize的調用之間沒有插入foo )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM