This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 610
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add function contains() to map data structure
- Loading branch information
1 parent
85fadac
commit b5740ef
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# contains | ||
|
||
**Description :** This method is available since C++20 and checks if there is an element with a specific key in the map container. Returns true if an element is found, otherwise returns false. | ||
|
||
**Example** : | ||
|
||
```cpp | ||
// C++ code to demonstrate working of map.contains() | ||
#include <iostream> | ||
#include <map> | ||
|
||
int main(){ | ||
// create 'example' map where 1 and 2 are keys, while a and b are values | ||
std::map<int,char> example = {{1,'a'},{2,'b'}}; | ||
int key = 2; | ||
|
||
// contains() will return true if the key was found, false if not | ||
if(example.contains(key)){ | ||
std::cout << "Found key " << key << " with value " << example[key] << '\n'; | ||
} | ||
else{ | ||
std::cout << "Key " << key << " not found in map\n"; | ||
} | ||
|
||
return 0; | ||
} | ||
``` | ||
**[Run Code](https://rextester.com/IYJR76647)** |