Skip to content
MapGetOrDefault

MapGetOrDefault

Description

Get the first value stored for a key, or return a fallback value copy.

This returns a value copy, not a pointer. The fallback value is not inserted into the map. If you want insertion-on-miss semantics, use MapEnsurePtr.

Parameters

Name Direction Description
m in,out Map.
lookup_key in Key to search for.
default_value in Value returned when key does not exist.

Success

Returns a value of type MAP_VALUE_TYPE(m) - either a copy of the first stored value for the key, or a copy of default_value when the key is absent. The map is not modified; default_value is not inserted.

Failure

Does not return on invalid arguments (caller bug); aborts via LOG_FATAL.

Usage example (Cross-references)

Usage examples (Cross-references)
    
        // MapGetOrDefault on an empty map returns the default, inserts nothing.
        int got = MapGetOrDefault(&map, 7, 1234);
        result  = result && (got == 1234);
        result  = result && (MapPairCount(&map) == 0);
        MapInsertR(&map, 11, 111);
    
        int  found  = MapGetOrDefault(&map, 11, 999);
        int  miss   = MapGetOrDefault(&map, 999, 555);
        bool result = (found == 110);
    
        int  found  = MapGetOrDefault(&map, 11, 999);
        int  miss   = MapGetOrDefault(&map, 999, 555);
        bool result = (found == 110);
        result      = result && (miss == 555);
Last updated on