how isKey searches keys of an associative map (containers.Map)

5 ビュー (過去 30 日間)
Herbert Triceratops
Herbert Triceratops 2021 年 9 月 23 日
回答済み: Samhitha 2025 年 4 月 23 日
If M is an associative map (containers.Map) and k is a potential key, what exactly happens when I execute isKey(M,k)? It seems that the output of keys(M) is ordered by whatever the "natural" order is for M's particular type of key. Does that mean that isKey does a binary search? Or does isKey do a linear search? Or does isKey do something else? Or does it depend on details that I didn't include here?
Thanks!

回答 (1 件)

Samhitha
Samhitha 2025 年 4 月 23 日
While executing isKey(M, k) on a containers.Map in MATLAB, the function checks for the existence of the key using a hash table lookup, not a linear or binary search. The order of keys returned by keys(M) is just for display and does not affect how “isKey” works. The key lookup is generally very fast average-case constant time and independent of key type or order.
For more details go through the following documentation

カテゴリ

Help Center および File ExchangeAnalysis of Variance and Covariance についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by