STL vector中的shrink_to_fit方法(32)
来源:互联网 发布:js页面加载完成后跳转 编辑:程序博客网 时间:2024/06/06 15:41
std::vector::shrink_to_fit
void shrink_to_fit();
请求容器降低其容量和size匹配。
该请求不具有约束力,容器可以自由地去执行其他的优化方案(capacity可以大于size)。//我查了一下网上说是该方法由编译器决定是否真正释放多余的内存,该方法值是提出请求,是否要实现由编译器说了算。
例子:
// vector::shrink_to_fit#include <iostream>#include <vector>using namespace std;int main (){ std::vector<int> myvector (100); std::cout << "1. capacity of myvector: " << myvector.capacity() << '\n'; cout<<"size="<<myvector.size()<<endl; myvector.resize(10); std::cout << "2. capacity of myvector: " << myvector.capacity() << '\n'; cout<<"size="<<myvector.size()<<endl; myvector.shrink_to_fit(); std::cout << "3. capacity of myvector: " << myvector.capacity() << '\n'; cout<<"size="<<myvector.size()<<endl; return 0;}结果截图:
This may cause a reallocation, but has no effect on the vector size and cannot alter its elements.
这可能导致重分配,但不会影响其size以及不能改变其元素。
Parameters
noneReturn value
noneExample
1234567891011121314151617
// vector::shrink_to_fit#include <iostream>#include <vector>int main (){ std::vector<int> myvector (100); std::cout << "1. capacity of myvector: " << myvector.capacity() << '\n'; myvector.resize(10); std::cout << "2. capacity of myvector: " << myvector.capacity() << '\n'; myvector.shrink_to_fit(); std::cout << "3. capacity of myvector: " << myvector.capacity() << '\n'; return 0;}
Possible output:
1. capacity of myvector: 1002. capacity of myvector: 1003. capacity of myvector: 10
Complexity
At most, linear in container size.与容器大小线性相关。
Iterator validity
If a reallocation happens, all iterators, pointers and references related to the container are invalidated.
如果发生重分配,所有的迭代器,指针以及引用都将失效。
否则,不会改变其有效性。
Data races
The container is modified.
容器将被修改。
如果发生重分配,所有容器内元素都将被修改。
Otherwise, no contained elements are accessed.
否则,元素不会被访问。
Exception safety
If the type of the elements is either copyable or no-throw moveable, there are no changes in the container in case of exception (strong guarantee).
如果元素的复制构造以及移动构造不会抛出异常,那么容器抛出异常的规则不变。
Otherwise, if an exception is thrown, the container is left with a valid state (basic guarantee).
否则,如果容器抛出异常,容器依旧承诺保持在有效状态。
//翻译的不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
2014-8-19
于GDUT
- STL vector中的shrink_to_fit方法(32)
- vector shrink_to_fit
- STL vector中的insert方法
- 实战c++中的vector系列--正确释放vector的内存(clear(), swap(), shrink_to_fit())
- 实战c++中的vector系列--正确释放vector的内存(clear(), swap(), shrink_to_fit())
- 实战c++中的vector系列--正确释放vector的内存(clear(), swap(), shrink_to_fit())
- STL vector中的begin方法(3)
- STL vector中的front方法(4)
- STL vector中的rbegin方法(5)
- STL vector中的cbegin()方法(6)
- STL vector中的crbegin方法(7)
- STL vector中的end方法(8)
- STL vector中的cend方法(9)
- STL vector中的rend方法(10)
- STL vector中的crend()方法(11)
- STL vector中的reserve()方法(15)
- STL vector中的push_back方法(17)
- STL vector中的clear方法(18)
- Android:Android开源框架:AndroidAnnotations
- hdu4970 Killing Monsters
- android:跳转,Intent,有无返回值
- 思想改变态度,习惯改变命运!2014.8.19
- 神奇的位运算(不会不行啊。。。被坑了好多题了。。。。)
- STL vector中的shrink_to_fit方法(32)
- C++ hash_map
- ArcGIS Engine中删除要素的几种方法总结
- andorid开源快速开发框架之AndroidAnnoatitions
- Linux输入子系统:事件的编码 -- event-codes.txt
- dojo小例子(25)在循环结束后再执行后续语句
- github ssh 设置
- python 编码规范及习惯写法范例
- <Android developer> Creating a Fragment