STL vectorの中のshrink_to_fit方法(32)

3942 ワード

原文の住所:http://www.cplusplus.com/reference/vector/vector/shrink_to_fit/
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 implemention is free to optimize others wise and leave the vector with a capacity greater than its 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() << '
'; cout<<"size="<<myvector.size()<<endl; myvector.resize(10); std::cout << "2. capacity of myvector: " << myvector.capacity() << '
'; cout<<"size="<<myvector.size()<<endl; myvector.shrink_to_fit(); std::cout << "3. capacity of myvector: " << myvector.capacity() << '
'; cout<<"size="<<myvector.size()<<endl; return 0; }
結果のスクリーンショット:
STL vector中的shrink_to_fit方法(32)_第1张图片
This may cause a reallocation、but has no effect on the vector size and cannot alter its elements.
これは再分配を引き起こすかもしれないが、そのsizeに影響を与えず、その要素を変えることができない.
パラメータ
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() << '
'; myvector.resize(10); std::cout << "2. capacity of myvector: " << myvector.capacity() << '
'; myvector.shrink_to_fit(); std::cout << "3. capacity of myvector: " << myvector.capacity() << '
'; return 0; }
Edit&Run
Possible output:
1. capacity of myvector: 100 2. capacity of myvector: 100 3. capacity of myvector: 10 
Coplexity
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 elemens are modified.
再割り当てが発生すると、すべての容器内の要素が修正されます.
Otherwise、no contained elemens are accessed.
要素はアクセスされません.
Exception safety
If the type of the elemens is exther copyable or no-throw moveable、there re no changes in the container in case of exception.
要素の複製構造や移動構造に異常がない場合は、容器から異常が出る規則は変わりません.
Otherwise、if an exception is thrown、the container is left with a valid state(baic garantee)
さもなくば、容器が異常を投げたら、容器は依然として有効な状態で維持することを承諾します.
//翻訳のよくないところをご指導ください.下のメッセージを残したり、左上のメールアドレスをクリックしてメールしてください.私の間違いと不足を指摘してください.修正して、もっと良いのを皆さんに共有できます.ありがとうございます.
転載は出典を明記してください.http://blog.csdn.net/qq844352155
2014-8-19
GDUTで