STL vector中的shrink_to_fit方法(32)

public member function
<vector>

std::vector::shrink_to_fit

void shrink_to_fit();
Shrink to fit
Requests the container to reduce its capacity to fit its size.

请求容器降低其容量和size匹配。


The request is non-binding, and the container implementation is free to optimize otherwise and leave the vector with a capacity greater than its 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

none

Return value

none

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 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: 100
2. capacity of myvector: 100
3. 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.

如果发生重分配,所有的迭代器,指针以及引用都将失效。


Otherwise, no changes.

否则,不会改变其有效性。


Data races

The container is modified.

容器将被修改。


If a reallocation happens, all contained elements are 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






  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值