- 论坛徽章:
- 0
|
- 1 #include <vector>
- 2 #include <map>
- 3 #include <string>
- 4 #include <cstdio>
- 5 #include <ctime>
- 6 #include <iostream>
- 7
- 8 using namespace std;
- 9
- 10 template<class X>
- 11 void tryFun(X& haha)
- 12 {
- 13 int i=1;
- 14 cout<<haha<<endl;
- 15 }
- 16
- 17 int main(int argc, char *argv[])
- 18 {
- 19 clock_t start=clock();
- 20 map<string,int> aaa;
- 21 char str[100];//内存使用0%
- 22
- 23 for( int i=0 ; i<10000000 ; ++i )
- 24 {
- 25 sprintf(str,"abcaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa%d",i);
- 26 aaa[str]=1;
- 27 }
- 28 map<string,int>::iterator it=aaa.begin();//内存使用19.6%
- 29 vector<string> abc;
- 30 for( int i=0 ; i<10000000 ; ++i )
- 31 {
- 32 abc.push_back(it->first);
- 33 ++it;
- 34 }
- 35
- 36
- 37 cout<<(clock() - start)/1000<<endl;//内存使用20%
- 38 EXIT:
- 39 cout<<"aaaa"<<endl;
- 40 return 0;
- }
复制代码 为什么vector的push_back几乎没有增加内存? |
|