I have a c++ map declared as follows
std::map<std::string, int> wordMap= {
{ "is", 6 },
{ "the", 5 },
{ "hat", 9 },
{ "at", 6 }
};
I would like to know how to find the number of distinct values of int
existing in wordMap
.
In this example, I would expect an output of 3 as i have 3 different distinct values (6,5,9)
.
Using sort function() Calculate the length of an array using the length() function that will return an integer value as per the elements in an array. Call the sort function and pass the array and the size of an array as a parameter. Take a temporary variable that will store the count of distinct elements.
Each element has a key value and a mapped value. No two mapped values can have same key values. In C++, size() function is used to return the total number of elements present in the map. Return Value: It returns the number of elements present in the map.
Multimap is similar to a map with the addition that multiple elements can have the same keys. Also, it is NOT required that the key-value and mapped value pair have to be unique in this case.
C++ Software Engineering Multi-map in C++ is an associative container like map. It internally store elements in key value pair. But unlike map which store only unique keys, multimap can have duplicate keys.
Try to use std::set for counting:
std::set<int> st;
for (const auto &e : wordMap)
st.insert(e.second);
std::cout << st.size() << std::endl;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With