我有一個在這種情況下,如何優化一對結構?
template<class Key, class Value, class Comp = std::less<Key> > class MyContainer;
其與節點的樹內部,其中,每個節點類型定義如下實施:
typedef std::pair<Key, Value> Node;
要擦除從所述容器的一個元件我需要提供下面的方法給用戶:
void MyContainer::erase(const Key& k) {
const Node n(k, default_value); // default_value is a data member, copied from Value() by default in the constructor of MyContainer
//...
mytree->remove(n); // search and remove the Node in case
}
樹用途(衍生自所述包裝類)less<Key>
進行比較和排序在T中的元素稀土元素。
如您所知,樹中的remove()
方法並不需要訪問該對的第二個元素來查找該節點。由於這個原因,我想避免default_value
副本的開銷到pair
的第二個元素。
有沒有辦法擺脫這種不必要的開銷?
編輯:類型的樹給出
template<class T, class Comp = std::less<T> > class Tree;
爲什麼你就不能添加,需要一個'const的關鍵&'?什麼類型'mytree'? – 2012-03-02 11:52:26
一個'remove'方法@David MyContainer是(一種)HashMap,其中每個桶是樹 – Martin 2012-03-02 12:07:01