当需要修改元素时,什么是替代的集合式数据结构? [英] What is an alternative set-like data structure when modifying elements is necessary?
本文介绍了当需要修改元素时,什么是替代的集合式数据结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
STL集合的设计使得不可能修改集合中的元素(有很好的理由,请参见 stackoverflow )。然而,假设我有一个结构,其中只有一个成员作为关键。有一个替代的数据结构到一个集合。我真的很想要集合的行为,除非这个无法修改非密钥的字段或成员。
STL sets are designed so that it is not possible to modify the elements in the set (with good reason, see stackoverflow). Suppose however I have a structure in which only one of the members acts as the key. Is there an alternative data structure to a set. I really want exactly the behavior of sets except this inability to modify a field or member which is non-key.
推荐答案
可能是现在是时候重新设计,考虑std :: map而不是std :: set。
May be it's time to redesign, considering std::map instead of std::set.
而不是
struct A
{
K key;
V val;
};
set<A> a;
考虑
std::map<K,V> m;
然后您可以执行
m[k] = v;
这篇关于当需要修改元素时,什么是替代的集合式数据结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文