Using the below C++
code prints different sizes of map before and after comparison.
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{
string str1 = "abc", str2 = "def";
map<char, int> mp;
for(char c: str1) mp[c]++;
cout << "Size of map: " << mp.size() << endl;
for(char ch: str2) {
if(mp[ch]) {
// do something...
}
}
cout << "Size of map: " << mp.size() << endl;
return 0;
}
Why does checking whether a key
exists in a map
increases the size of the map
?