为什么shrink_to_fit是非绑定的? [英] Why is shrink_to_fit non-binding?

查看:264
本文介绍了为什么shrink_to_fit是非绑定的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 0x FCD在23.3.6.2向量容量中的状态:

The C++0x FCD states in 23.3.6.2 vector capacity:


void shrink_to_fit();

注释:shrink_to_fit是一个非绑定请求,以减少capacity()size()。 [注意:请求是非绑定的,以允许实现特定于优化的纬度。 — end note]

Remarks: shrink_to_fit is a non-binding request to reduce capacity() to size(). [Note: The request is non-binding to allow latitude for implementation-specific optimizations. —end note]

要允许什么优化?

推荐答案

这是一个相当紧张,但是:

This is quite a strained out, but:

考虑vector只能分配内存的分配器, 。然后,如果向量具有4096的容量和4095的大小,则这将不重要分配存储器,因为这将不节省存储器,但浪费一些CPU时间来复制元素。

Consider vector's allocator that could only allocate memory with, say, 4 KB granularity. Then it wouldn't make sense to reallocate memory if a vector had capacity of 4096 and size of 4095 as this wouldn't conserve memory, yet waste some CPU time for copying elements.

这篇关于为什么shrink_to_fit是非绑定的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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