Ordered Map
Insert, find, erase
OpenImplement every member: insert, operator[], find, contains, erase, lower_bound, for_each, size, empty, clear and the destructor.
The keys in this Part arrive in random order, a few thousand at a time. The tests compare every operation against a reference model, erase keys in an order unrelated to the order they were inserted, and count every key and value the map constructs and destroys. One key type provides operator< and no other comparison.
Implement Map<K, V>, an ordered associative container that mirrors the core of std::map<K, V>: unique keys ordered by operator<, logarithmic lookups, inserts and erases, and traversal in key order.
template <class K, class V>
class Map {
public:
Map() noexcept;
~Map();
Map(const Map&) = delete;
Map& operator=(const Map&) = delete;
bool insert(const K& key, const V& value); // false, and no change, if key is present
V& operator[](const K& key); // inserts V() first if key is missing
V* find(const K& key); // nullptr if key is missing
const V* find(const K& key) const;
bool contains(const K& key) const;
bool erase(const K& key); // false if key is missing
const K* lower_bound(const K& key) const; // smallest stored key not less than key, or nullptr
template <class F>
void for_each(F&& visit) const; // visit(key, value) per entry, ascending
std::size_t size() const noexcept;
bool empty() const noexcept;
void clear() noexcept;
};- Keys are compared with
operator<and nothing else. Two keys are equivalent when neither is less than the other. One of the key types in the tests providesoperator<only; every other comparison operator on it fails the test that triggers it. insertnever overwrites: inserting a key that is present returnsfalseand leaves the stored value alone.operator[]constructs a value-initializedV()only when the key is missing, and returns a reference to the stored value either way.findandlower_boundreturn pointers into the map. A pointer stays valid until its entry is erased or the map is cleared or destroyed.for_eachcallsvisit(key, value)exactly once per entry, with keys in ascending order, passing both as const references.- Only the entries the map holds exist: each stored key and value is constructed once when its entry is added and destroyed once when the entry is erased, cleared or destroyed with the map. The tests count both, and abort on a double destroy.
- Every operation runs in time in the worst case, and
for_eachin . Part 1 feeds keys in random order; Part 2 feeds them sorted, counts every key comparison, copy, move and assignment the map makes, and fails a scenario as soon as those outgrow .
K and V are copy-constructible with non-throwing destructors, and V is default-constructible. Neither needs to be assignable for insert, find, erase or for_each.
The harness defines the standard containers away: vector, deque, list, forward_list, array, valarray, map, multimap, set, multiset, unordered_map, unordered_multimap, unordered_set and unordered_multiset may not appear in a submission, member and variable names included. Do not write main; the harness owns it. The standard headers are already included.
Map<std::string, int> ages;
ages.insert("bo", 31); // true
ages.insert("bo", 99); // false, "bo" still maps to 31
ages["al"] = 28; // inserts "al" -> 0, then assigns 28
*ages.lower_bound("b"); // "bo"
ages.for_each([](const std::string& k, const int& v) {
// ("al", 28), then ("bo", 31)
});
ages.erase("al"); // trueNone. The judge compiles your class into a hidden harness that drives it directly and reports one row per scenario.
None. Your code prints nothing; the harness owns the verdict.
Single-threaded. No iterators:
for_eachandlower_boundcover traversal and ordered lookup.inserttakes a key and a value rather than a pair, and returns only whether the key was new.
- At most entries at once and operations per scenario.
insert-find-erase
PASS