std :: multimap的使用案例 [英] Use cases of std::multimap
问题描述
我不太明白这个数据结构的目的。 std :: multimap< K,V>
和 std :: map< K,std :: vector< V& / code>。这同样适用于
std :: multiset
- 它可以只是 std :: map< K,int>
int计数了K的出现次数。我在这些结构的使用上缺少了什么?
考虑在按名称分组的AdressList中的PhoneEntry。
int AdressListCompare(const PhoneEntry& p1,const PhoneEntry& p2){
return p1.name< p2.name;
}
multiset< PhoneEntry,AdressListCompare> adressList;
adressList.insert(PhoneEntry(Cpt.G,123-456,Cellular));
adressList.insert(PhoneEntry(Cpt.G,234-567,Work));
//获取条目
addressList.equal_range(PhoneENtry(Cpt.G)); //所有数字
这不适用于set + count。如果不需要这种行为,你的Object + count方法似乎更快。例如multiset :: count()成员状态
复杂性:大小对数对数+ / p>
I don't quite get the purpose of this data structure. What's the difference between std::multimap<K, V>
and std::map<K, std::vector<V>>
. The same goes for std::multiset
- it could just be std::map<K, int>
where the int counts the number of occurrences of K. Am I missing something on the uses of these structures?
A counter-example seems to be in order.
Consider a PhoneEntry in an AdressList grouped by name.
int AdressListCompare(const PhoneEntry& p1, const PhoneEntry& p2){
return p1.name<p2.name;
}
multiset<PhoneEntry, AdressListCompare> adressList;
adressList.insert( PhoneEntry("Cpt.G", "123-456", "Cellular") );
adressList.insert( PhoneEntry("Cpt.G", "234-567", "Work") );
// Getting the entries
addressList.equal_range( PhoneENtry("Cpt.G") ); // All numbers
This would not be feasible with a set+count. Your Object+count approach seems to be faster if this behavior is not required. For instance the multiset::count() member states
"Complexity: logarithmic in size + linear in count."
这篇关于std :: multimap的使用案例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!