1. What is the mapping type?
The mapping type is similar to the key-value relationship of PHP array. The difference is that after the mapping relationship is defined, the data types of key and value are fixed and cannot be changed. Mapping is usually used to store data in contracts, such as storing user addresses and their token balances, or storing user addresses and their levels.
2. How to define and access mapping types
The mapping type is defined by the mapping field. The key type cannot be a variable-length array, contract type, or nested type, but there is no restriction on the value type.
Mapping can be accessed through the contract address, for example balance[userAddr] . Note that if this mapping is accessed through a non-existent key, the default value of the value type will be returned.
3. Limitations of mapping
Mapping can only be used as a state variable (in PHP, it can only be defined in the member variables of the class, not in the function)
Mapping cannot be traversed, has no length, no key set, and no value set. It is just a basic data type. If you want to implement richer functions, you can only use additional class libraries to implement related functions, such as defining a structure and corresponding operation functions yourself.
struct itmap
{
mapping(address => uint) data;
uint size;
}
3. Practice by writing code
