- Mastering C++ Programming
- Jeganathan Swaminathan
- 190字
- 2021-07-02 18:28:52
Map
A map stores the values organized by keys. Unlike a set, a map has a dedicated key per value. Maps generally use a red-black tree as an internal data structure, which is a balanced BST that guarantees O( log N ) runtime efficiency for searching or locating a value in the map. The values stored in a map are sorted based on the key, using a red-black tree. The keys used in a map must be unique. A map will not retain the sequences of the input as it reorganizes the values based on the key, that is, the red-black tree will be rotated to balance the red-black tree height.
Let's write a simple program to understand map usage:
#include <iostream>
#include <map>
#include <iterator>
#include <algorithm>
using namespace std;
int main ( ) {
map<string, long> contacts;
contacts["Jegan"] = 123456789;
contacts["Meena"] = 523456289;
contacts["Nitesh"] = 623856729;
contacts["Sriram"] = 993456789;
auto pos = contacts.find( "Sriram" );
if ( pos != contacts.end() )
cout << pos->second << endl;
return 0;
}
Let's compile and check the output of the program:
g++ main.cpp -std=c++17
./a.out
The output is as follows:
Mobile number of Sriram is 8901122334
推薦閱讀
- scikit-learn Cookbook
- Mastering RabbitMQ
- C語言程序設計(第2 版)
- C#程序設計(慕課版)
- Windows Server 2012 Unified Remote Access Planning and Deployment
- BeagleBone Black Cookbook
- C#開發案例精粹
- Scratch趣味編程:陪孩子像搭積木一樣學編程
- 從零開始學Android開發
- 區塊鏈架構之美:從比特幣、以太坊、超級賬本看區塊鏈架構設計
- HTML+CSS+JavaScript網頁制作:從入門到精通(第4版)
- MySQL 8從零開始學(視頻教學版)
- 軟件測試技術
- TypeScript全棧開發
- C語言從入門到精通(第5版)