自定义数据结构与STL map对比

自定义数据结构实现形如dict["index"]="value"索引数组功能.
目前仅仅是有个大概功能,暂时未加入模板.
先看看代码吧.获取时间是用windows.h中的GetSystemTime()函数.

不过看到自己实现的运行时间和STL map的时间还真是差别大啊...

还得好好努力...

 

   1.  //test.cpp
   2. #include "dict.h"  
   
3. #include <Windows.h>  
   
4. #include <map>  
   
5using namespace std;  
   
6.   
   
7string ita(int t)  
   
8{  
   
9.     int pos = t,i=0;   
  
10.     string temp("");   
  
11.     do   
  
12.        {    
  
13.            i = t%10;  
  
14.            t = (int)(t/10);  
  
15.            temp += '0'+i;    
  
16.        }
 while (t>0);   
  
17.    return temp;  
  
18. }
  
  
19void main()  
  
20{  
  
21.   
  
22.     SYSTEMTIME start,end;  
  
23.   
  
24#pragma region mydict  
  
25.     dict  * dt = new dict();  
  
26.     GetSystemTime(&start);  
  
27.     for (int i=0;i<1000;i++)  
  
28.     {    
  
29.         dt->Add("索引"+ita(i),""+ita(i));  
  
30.     }
     
  
31.     dt->print();   
  
32//  cout<<"dt[\"索引1\"]:"<< (*dt)["索引1"]<<endl;  
  33//  dt->Remove("索引1);  
  34//  dt->print();  
  35//  (*dt)["我们"]="我们的";  
  36//     (*dt)["索引1"]="索引13";  
  37//  dt->print();  
  38//  cout<<(*dt)[1]<<endl;  
  39//  (*dt)[1]="dream";  
  40//  cout<<(*dt)[1]<<endl;     
  41.     GetSystemTime(&end);  
  
42.     delete dt;  
  
43.     int startTime = start.wMinute*60*1000+start.wSecond*1000+start.wMilliseconds;  
  
44.     int endTime = end.wMinute*60*1000+end.wSecond*1000+end.wMilliseconds;  
  
45.     cout<<"start at:"<<dec<<startTime<<",and ends at:"<<endTime<<",process cost:"<<endTime-startTime<<"ms"<<endl; 
  
46#pragma endregion mydict 
  
47.  
  
48#pragma region stlMap 
  
49.  
  
50.     map<string,string> mp; 
  
51.     GetSystemTime(&start); 
  
52.     for (int i=0;i<1000;i++
  
53.     {    
  
54.         mp["索引"+ita(i)]=""+ita(i); 
  
55.     }
    
  
56.     map<string,string>::iterator its;    
  
57.     cout<<"下面是所有的键值(共有"<<mp.size()<<"项):"<<endl; 
  
58.     int iPos=0
  
59.     for(its = mp.begin(); its !=mp.end() ; its++,iPos++
  
60.         cout<<"map"<<iPos<<"["<< (*its).first<<"]="<<(*its).second<<";"<<endl;  
  
61.     cout<<"ok"<<endl;  
  
62.     GetSystemTime(&end); 
  
63.     startTime = start.wMinute*60*1000+start.wSecond*1000+start.wMilliseconds; 
  
64.     endTime = end.wMinute*60*1000+end.wSecond*1000+end.wMilliseconds; 
  
65.     cout<<"start at:"<<dec<<startTime<<",and ends at:"<<endTime<<",process cost:"<<endTime-startTime<<"ms"<<endl;  
  
66#pragma endregion stlMap  
  
67. }
  

其他代码见附件.http://imzc.net/show-93-1.html


posted @ 2009-06-27 10:53  O(∩_∩)O川zc  阅读(355)  评论(0编辑  收藏  举报