stlまたは一般に、一種の「逆」連想コンテナが存在しますか? たとえば、同じ要素が一連のキーによって共有されるコンテナが必要です。
私のキーが であるとしましょう。たとえば、次のint
ようになります。
container.at(3) -> some object A
container.at(4) -> same object A
container.at(1) -> other object B
このコンテナーは (理想的には) さまざまな操作の std::map と同じ複雑さを持ちます。そのようなことは可能ですか?
最初は複数のインデックスが同じオブジェクトを指している a を使用することを考えていましたstd::map<int, T*>
が、マップからアイテムを削除すると、実行時間が O(n) になります。これは、他のアイテムをチェックして、 Tを削除する必要がありますobject
stlまたはboostに「ネイティブに」この種のコンテナがすでにありますか?
編集:使用例:
container<int, myClass> myContainer;
myClass obj(...); //new object
myContainer.insert(3, obj); //insert object for specific key
myContainer.insert(2, obj); //insert same object for specific key, since both objects would compare equal we would actually not "insert" a new object but have it shared by both keys
myContainer.duplicate_object(2,5); //key 5 also has the same object as key 2 (and 3)
myContainer.getAllKeys(2); //would return 2,3 and 5 since they all reference the same object as key 2
myContainer.removeKey(3);
myContainer.removeKey(2);
myContainer.removeKey(5); //would destroy the object here, not before