C++:标准::矢量::调整大小与 "normal"分配

C++: std::vector::resize vs. "normal" allocation

本文关键字:分配 normal 标准 矢量 调整 C++      更新时间:2023-10-16

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)。

没有区别,只是后者比稍微更高效和简洁。