C++:标准::矢量::调整大小与 "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
)。
没有区别,只是后者比稍微更高效和简洁。
相关文章:
- 将数组的地址分配给变量并删除
- vector.resize()中的分配错误
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- Win32编译器选项和内存分配
- 函数中堆分配的效果与缺少堆分配的情况
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 获取字符串的长度并将其分配给数组
- 将地址分配给本地指针后,公共对象的变量将消失
- 递归模板化函数不能分配给具有常量限定类型"const tt &"的变量"state"
- 有没有一种方法可以使用placement new将堆叠对象分配给分配的内存
- 我在二维向量中是否正确分配了内存
- 正在尝试重载二进制搜索树分配运算符
- GlobalAlloc而不是其他分配方法
- 自定义先决条件对移动分配运算符有效吗
- 我可以重新分配/覆盖std::字符串吗
- 在c++中使用动态分配的问题
- 当一个新对象被分配到它的地址时,对象是否必须被销毁
- 为什么我可以使用比分配的内存更多的内存
- C++:标准::矢量::调整大小与 "normal"分配