STL容器在堆栈和堆上 [英] STL containers on the stack and the heap

查看:330
本文介绍了STL容器在堆栈和堆上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果std :: vector和friends是自我调整大小,这是否意味着如果我声明一个像这样的向量:

  std :: vector< string> myvec; 

然后它会使用更多堆栈调整大小,而:

  std :: vector< string> * myvec = new std :: vector< string>(); 

会使用更多堆调整大小吗?



在堆栈中为基于堆栈的主体支付的唯一的东西是一个几个字节,内部缓冲区总是从堆中分配。



当你做一个vec = new vector()可能不是很好。


If std::vector and friends are self resizing, does that mean if I declare a vector like so:

std::vector<string> myvec;

Then it'll resize using more stack, whereas:

std::vector<string> *myvec = new std::vector<string>();

Would resize using more heap?

解决方案

Vectors allocate on the heap in their internals.

The only thing you pay for in the stack for a stack based bector is a couple of bytes, the inner buffer will always be allocated from the heap.

So effectively when you do a vec = new vector() you are allocating a small quantity, which may not be really good.

这篇关于STL容器在堆栈和堆上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆