標籤:stream begin namespace air ring ++ 控制台 java fprintf
map作為最常用的資料結構之一,用的好可以大幅度的提升效能。
// java_cpp_perftest.cpp : 定義控制台應用程式的進入點。//#include "stdafx.h"#include <map> //map#include <string>#include <iostream>#include <objbase.h> // uuid使用到using namespace std;#define GUID_LEN 64 int _tmain(int argc, _TCHAR* argv[]){ int i,j; // map定義 map<int, string> mapStudent; for (i=0;i<10000;i++) { // 建立UUID, vc專用, linux通用的可參考http://www.cnblogs.com/lidabo/p/3602038.html char buffer[GUID_LEN] = { 0 }; GUID guid; if ( CoCreateGuid(&guid) ) { fprintf(stderr, "create guid error\n"); return -1; } _snprintf_s(buffer, sizeof(buffer), "%08X-%04X-%04x-%02X%02X-%02X%02X%02X%02X%02X%02X", guid.Data1, guid.Data2, guid.Data3, guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]); // map插入 mapStudent.insert(pair<int, string>(i, buffer)); } // 擷取時間相對計數器, vc專用 DWORD begin = GetTickCount(); int f; for (j=0;j<100;j++) { for (f=0;f<10000;f++) { // map尋找 mapStudent.find(f); } } DWORD end = GetTickCount(); // 列印時間差 cout << (end - begin) << endl; //就我們VS2012測試來看, 這一步居然要4s左右, 相同情況下,JDK 8隻用了20ms // map遍曆 map<int, string>::iterator iter; for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++) { cout << iter->first << " " << iter->second << endl; } system("pause");}
c++學習之map基本操作