From 84c27f7db109f266cc47509452a4a7641d7a54cc Mon Sep 17 00:00:00 2001 From: "Coo.King" <39448470+Pega-Stellar@users.noreply.github.com> Date: Tue, 16 Oct 2018 11:27:31 +0700 Subject: [PATCH] Add function to "Map Manual" (#19043) * Update index.md * Update index.md * Update index.md --- .../guide/english/cplusplus/map/index.md | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/client/src/pages/guide/english/cplusplus/map/index.md b/client/src/pages/guide/english/cplusplus/map/index.md index 14772b23bf..41e8d36146 100644 --- a/client/src/pages/guide/english/cplusplus/map/index.md +++ b/client/src/pages/guide/english/cplusplus/map/index.md @@ -50,6 +50,19 @@ d => 40 ## Creating map object ` map myMap; ` +## Get Size +Get size of map with size function +``` +map myMap; +myMap[100] = 3 +count << "size of map is " << myMap.size() << '\n'; +``` + +Output: +``` +size of map is 1 +``` + ## Insertion Inserting data with insert member function. @@ -62,6 +75,42 @@ We can also insert data in std::map using operator [] i.e. `myMap["sun"] = 3;` +If "sun" is already mapped before, this action will override the value mapped to key. + +## Erase +Erasing data with erase function + +``` +map myMap; +myMap[10] = 1000; +cout << "before erase, size of map is " << myMap.size() << '\n'; +myMap.erase(10); +cout << "after erase, size of map is " << myMap.size() << '\n'; +``` + +Output: +``` +before erase, size of map is 1 +after erase, size of map is 0 +``` + +## Accessing map value + +To access map values, simply call Map[key]. For example: +``` +map M; +M["abc"] = 1; +M["def"] = 2; +cout << "value of abc is " << M["abc"] << '\n'; +cout << "value of def is " << M["def"] << '\n'; +``` + +Output: +``` +value of abc is 1 +value of def is 2 +``` + ## Accessing map elements To access map elements, you have to create iterator for it. Here is an example as stated before.