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';
name = {'dad','mom'};
number = [123 321];
map = containers.Map(a,b)
These three attributes are the number, the type of bond, and the type of the key, the key type of attention must be char.
- access
kk = map('dad'); % 获得dad 的电话号码
2. Modify
map('dad') = '123456789'
- delete
map.remove(dad); % 删除键 dad 和对应的键值
containers.Map features:
- We can continue to expand and does not require pre-allocated memory
- You can enhance code readability
- It can provide data to quickly find
containers.Map in more ways :