forked from Bhupesh-V/30-seconds-of-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Bhupesh-V#480 from Manali21Gupta/pgm_clear
unordered_map clear function
- Loading branch information
Showing
1 changed file
with
34 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,34 @@ | ||
# clear | ||
|
||
**Description :** This function is used to remove all elements from the container. | ||
|
||
**Example** : | ||
|
||
```cpp | ||
// Demonstrates clear() | ||
#include <iostream> | ||
#include <unordered_map> | ||
|
||
int main(){ | ||
//declares an empty map. O(1) | ||
std::unordered_map<char, int> my_map; | ||
|
||
// inserting in to unordered_map with O(1) time on average | ||
my_map.insert({'A', 1}); | ||
my_map.insert({'B', 2}); | ||
my_map.insert({'C', 3}); | ||
|
||
//Print the size of the container | ||
std::cout << "Size of unordered_map before calling clear function: " << my_map.size() << std::endl; | ||
|
||
//Deleting all elements by calling clear function | ||
my_map.clear(); | ||
|
||
//Print the size of the container | ||
std::cout << "Size of unordered_map after calling clear function: " << my_map.size() << std::endl; | ||
|
||
return 0; | ||
} | ||
|
||
``` | ||
**[Run Code](https://rextester.com/BQLV19570)** |