官术网_书友最值得收藏!

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
主站蜘蛛池模板: 博野县| 双城市| 石楼县| 万荣县| 三都| 镇康县| 滨州市| 元朗区| 大港区| 汕尾市| 吐鲁番市| 芦溪县| 晋宁县| 厦门市| 桃园市| 磐安县| 宁夏| 都昌县| 黄陵县| 加查县| 满城县| 津南区| 新竹市| 孟村| 汉阴县| 榆社县| SHOW| 克什克腾旗| 兴宁市| 平昌县| 桑植县| 昂仁县| 剑河县| 荔波县| 长汀县| 古田县| 桑日县| 潍坊市| 滦南县| 永寿县| 霍山县|