c++學習之map基本操作

來源:互聯網
上載者:User

標籤: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基本操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.