- 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
推薦閱讀
- UI設(shè)計(jì)基礎(chǔ)培訓(xùn)教程
- JavaScript從入門到精通(微視頻精編版)
- FreeSWITCH 1.8
- CockroachDB權(quán)威指南
- Effective C#:改善C#代碼的50個(gè)有效方法(原書第3版)
- 算法精粹:經(jīng)典計(jì)算機(jī)科學(xué)問題的Java實(shí)現(xiàn)
- Programming ArcGIS 10.1 with Python Cookbook
- Internet of Things with Intel Galileo
- Expert Data Visualization
- 第一行代碼 C語言(視頻講解版)
- 基于ARM Cortex-M4F內(nèi)核的MSP432 MCU開發(fā)實(shí)踐
- Odoo 10 Implementation Cookbook
- SQL Server 2016 從入門到實(shí)戰(zhàn)(視頻教學(xué)版)
- Delphi開發(fā)典型模塊大全(修訂版)
- JavaScript編程精解(原書第2版)