MATLAB advanced data types containers.Map

Under normal circumstances, we use the basic types of cellular structures including arrays, etc., to meet the basic needs, but they also have limitations, we will sometimes use containers.Map to make better readable codes , higher efficiency of the procedure. containers.Map dictionary-like in Python, it has mapped one to one relationship between the two elements.

Suppose now that we need to do a phone book, then name and phone number should be one to one, if struct or cell type store, there will be a problem, index all of the corresponding elements must be the same, you want to get the corresponding person's phone number, first of all there is no query this person, then get the person's needs, by serial number to obtain the corresponding phone number, is a complex process, and the other is the order every time to find inefficiencies, and therefore It is recommended to use containers .Map data type.

containers.Map is encapsulated in a class, and use the ordinary type have many similarities.

map = containers.Map; % 声明了一个对象
map('dad') = '15536952269';

Here Insert Picture Description

name = {'dad','mom'};
number = [123 321];
map = containers.Map(a,b)

Here Insert Picture Description
These three attributes are the number, the type of bond, and the type of the key, the key type of attention must be char.

  1. access
kk = map('dad'); % 获得dad 的电话号码

Here Insert Picture Description
2. Modify

map('dad') = '123456789'
  1. delete
map.remove(dad); % 删除键 dad 和对应的键值

containers.Map features:

  1. We can continue to expand and does not require pre-allocated memory
  2. You can enhance code readability
  3. It can provide data to quickly find

containers.Map in more ways :
Here Insert Picture Description

Published 58 original articles · won praise 69 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_43157190/article/details/104720856
Recommended