Add function to "Map Manual" (#19043)
* Update index.md * Update index.md * Update index.md
This commit is contained in:
@ -50,6 +50,19 @@ d => 40
|
|||||||
## Creating map object
|
## Creating map object
|
||||||
` map<string, int> myMap; `
|
` map<string, int> myMap; `
|
||||||
|
|
||||||
|
## Get Size
|
||||||
|
Get size of map with size function
|
||||||
|
```
|
||||||
|
map<int, int > myMap;
|
||||||
|
myMap[100] = 3
|
||||||
|
count << "size of map is " << myMap.size() << '\n';
|
||||||
|
```
|
||||||
|
|
||||||
|
Output:
|
||||||
|
```
|
||||||
|
size of map is 1
|
||||||
|
```
|
||||||
|
|
||||||
## Insertion
|
## Insertion
|
||||||
Inserting data with insert member function.
|
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;`
|
`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<int, int > 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<string, int > 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
|
## Accessing map elements
|
||||||
|
|
||||||
To access map elements, you have to create iterator for it. Here is an example as stated before.
|
To access map elements, you have to create iterator for it. Here is an example as stated before.
|
||||||
|
Reference in New Issue
Block a user