Ordered Map

Normal+ 4.5 Exclusive 15s 512 MB
Data StructuresMaps
Part 1

Insert, find, erase

Open

Implement 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.

C++
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 provides operator< only; every other comparison operator on it fails the test that triggers it.
  • insert never overwrites: inserting a key that is present returns false and leaves the stored value alone. operator[] constructs a value-initialized V() only when the key is missing, and returns a reference to the stored value either way.
  • find and lower_bound return pointers into the map. A pointer stays valid until its entry is erased or the map is cleared or destroyed.
  • for_each calls visit(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 O(log⁡n)O(\log n) time in the worst case, and for_each in O(n)O(n). 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 O(nlog⁡n)O(n \log n).

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.

C++
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");               // true

None. 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_each and lower_bound cover traversal and ordered lookup.

  • insert takes a key and a value rather than a pair, and returns only whether the key was new.

  • At most 2⋅1052 \cdot 10^5 entries at once and 3⋅1053 \cdot 10^5 operations per scenario.
Accepted 1/1
Acceptance 100%
Loading editor...
Input
insert-find-erase
Expected output
PASS