??????Vector?????
???????????? ???????[ 2013/6/7 9:57:43 ] ????????
??????????????? ????????????????Vector???й???????? Link????
???????????? Vector?洢?Щ????????????????? clear() ?????沢??????????????????????й??????????????
????“vector ?? clear ????? capacity ?? ????? swap ?????? vector??”
???????????????????????????????????????? vector swap clear ??????????????????????????????????Щ?????????
??????????????????? ??Effective STL???е?“????17”????????
??????vector??string?????????????????????????????????????????????clear?? ????и??????????????capacity????????????????????檔 ??????????????????????????????????????????????С??????? ???????裨shrink to fit??
??????Effective STL?????????????????
vector<type> v;
//.... ??????????????v
//.... ???????v?е???????
vector<type>(v).swap(v);
//???v???????????????????????????????????
//????string???????????????
string(s).swap(s);
???????????????????????????vector??£??????????????????? ?????????????С????????????????????????????????vector v???? ????????????????н?????????????????????????????????v?????? ????????????????????????????????????????vector????????????????
?????????????????д????????????£?
#include <iostream>
#include <vector>
using namespace std;
vector <string> v;
char ch;
int main ()
{
for(int i=0; i<1000000; i++)
v.push_back("abcdefghijklmn");
cin >> ch;
// ???????????? ???54M
v.clear();
cin >> ch;
// ?????μ?飬 ??????54M
cout << "Vector ?? ?????" << v.capacity() << endl;
// ???????? 1048576
vector<string>(v).swap(v);
cout << "Vector ?? ?????" << v.capacity() << endl;
// ????????0
cin >> ch;
// ?????棬????? 10M+ ??????????
return 0;
}
???????
???????????????????????STL????????????? ????vector??????????ò??? clear ??????
??????????? ????STL???????????Щ???????????vector?? vector???????????????????????????????????????? ???·??????ɡ?
??????????????????????????????棬????????????????????????????? ?????????????μ???????????????? vector???????????????????? ?????????????????
?????????????????????? clear() ??? swap() ?ɡ?
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11