ssercpp12 / Homework

Homework result for 12 sser cpp
1 stars 8 forks source link

About Vector::clear() #23

Closed HuangLK closed 11 years ago

HuangLK commented 11 years ago

刷微博看到一个关于vector的帖子 http://blog.jobbole.com/37700/ ,也就是说标准的vector::clear() 是不释放容器内存的。 我想问的是,这样的设计有何好处?用户选择使用clear不就意味着清空这个容器重新使用了么?实现者有什么理由确信 保留这块内存 是最佳选择?而不是释放内存真正的清空容器? 由于个人搜索能力有限未能在各网站找到答案。。 另外,我们做这个vector的时候是否一定要按照 标准STL 的说明来实现?比如我的clear是有释放内存的,这样做应该也不会让用户无法使用,只是内部的内存分配上不太一样。这样似乎也算达到要求了吧?

best regards.

omegaga commented 11 years ago

You don't have to free the memory because clients intended to reuse the vector after clearing the vector. If we just reset the size of the vector, it is obvious that it will be much faster than freeing memory and then allocating again. (Only personal perspective, I'm also not sure if it is correct XD) Of course, you don't have to follow STL. You just need to make your own vector behaves like STL ones. And since our version of vector is simplified, your vector may even perform better.

HuangLK commented 11 years ago

Ok,thank for your reply.


Best Regards. Liankai Huang

------------------ 原始邮件 ------------------ 发件人: "OMeGa Lee"; 发送时间: 2013年6月4日(星期二) 中午12:06 收件人: "ssercpp12/Homework"; 抄送: "ACE"249092373@qq.com; 主题: Re: [Homework] About Vector::clear() (#23)

You don't have to free the memory because clients intended to reuse the vector after clearing the vector. If we just reset the size of the vector, it is obvious that it will be much faster than freeing memory and then allocating again. (Only personal perspective, I'm also not sure if it is correct XD) Of course, you don't have to follow STL. You just need to make your own vector behaves like STL ones. And since our version of vector is simplified, your vector may even perform better.

— Reply to this email directly or view it on GitHub.