为矢量分配内存 [英] Allocate memory for a vector
本文介绍了为矢量分配内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以给我一个例子,如何为矢量分配内存?几行就是我需要的。
我有一个向量,它接受20-30元素..但是当我尝试cout它和编译它只有第一对条目..
Can someone give me an example of how to allocate memory for a vector? A couple of lines is all I need. I have a vector that takes in 20-30 elements.. but when i try to cout it and compile it i only get the first couple of entries..
推荐答案
std :: vector 管理自己的内存。您可以使用 reserve()和 resize()方法让它分配足够的内存以适合给定数量的项:
An std::vector manages its own memory. You can use the reserve() and resize() methods to have it allocate enough memory to fit a given amount of items:
std::vector<int> vec1;
vec1.reserve(30); // Allocate space for 30 items, but vec1 is still empty.
std::vector<int> vec2;
vec2.resize(30); // Allocate space for 30 items, and vec2 now contains 30 items.
这篇关于为矢量分配内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文