M
Michael Klatt
I need an efficient insert-or-replace algorithm based on the key_type
of a std::map. The user needs to be notified if a replacement occurs.
This is what I have:
class Key;
typedef map<Key, bool> Map;
Map container;
Key some_key;
Map::value_type element(some_key, false);
// insert 'element', replacing exisiting element if necessary
pair<Map::iterator, bool> insertion(container.insert(element)); //
O(log N)
if (!insertion.second)
{
// replace existing element
cout << "replacing " << it->first << " with " << some_key << "\n";
Map::iterator it(container.erase(insertion.first)); // O(constant)
container.insert(it, element); // O(constant)
}
I think this will have a complexity approaching O(log N), meaning that
this is as efficient as I can hope for, correct?
of a std::map. The user needs to be notified if a replacement occurs.
This is what I have:
class Key;
typedef map<Key, bool> Map;
Map container;
Key some_key;
Map::value_type element(some_key, false);
// insert 'element', replacing exisiting element if necessary
pair<Map::iterator, bool> insertion(container.insert(element)); //
O(log N)
if (!insertion.second)
{
// replace existing element
cout << "replacing " << it->first << " with " << some_key << "\n";
Map::iterator it(container.erase(insertion.first)); // O(constant)
container.insert(it, element); // O(constant)
}
I think this will have a complexity approaching O(log N), meaning that
this is as efficient as I can hope for, correct?