[Qt-interest] New container class needed in Qt
Stephen Kelly
steveire at gmail.com
Wed Oct 19 09:44:21 CEST 2011
Mandeep Sandhu wrote:
> True. Performance is really not an issue with such few items. It's
> just that code-wise it looked a little 'iffy' to iterate the list and
> get the right element.
Why don't you just follow Scotts suggestion?
template<typename T, typename U>
class StackedHash
{
public:
void insert(T t, U u)
{
// ... TODO: What happens if t is already in m_hash?
m_hash.insert(t, u);
m_list.append(t);
}
U at(int i)
{
T t = m_list.at(i);
return m_hash.value(t);
}
private:
QHash<T, U> m_hash;
QVector<T> m_vector;
};
In other words: Create the container you crave by wrapping other containers.
Don't litter your code with keeping the lists and hashes in sync all over
the place. Do it in an encapsulation, just like you're looking for.
More information about the Qt-interest-old
mailing list