Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the structure and mapping of Solidity

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "what is Solidity structure and mapping". In daily operation, I believe many people have doubts about what Solidity structure and mapping is. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "what is Solidity structure and mapping". Next, please follow the editor to study!

Structure (Structs)

Solidity provides struct to define custom types, which are reference types. Let's look at the following example:

Pragma solidity ^ 0.4.11 X contract CrowdFunding {/ / defines a new type struct Funder {address addr; uint amount;} struct Campaign {address beneficiary; uint fundingGoal; uint numFunders; uint amount; mapping (uint = > Funder) funders;} uint numCampaigns; mapping (uint = > Campaign) campaigns with two members Function newCampaign (address beneficiary, uint goal) public returns (uint campaignID) {campaignID = numCampaigns++; / / campaignID returns / / creates a structure instance as a variable, stores it in storage and puts it in mapping campaigns [campaignID] = Campaign (beneficiary, goal, 0,0);} function contribute (uint campaignID) public payable {Campaign storage c = campaigns [campaignID] / / create a structure reference with mapping counterpart / / it can also be initialized with Funder (msg.sender, msg.value). C.funders [c.numFundersgiving +] = Funder ({addr: msg.sender, amount: msg.value}); c.amount + = msg.value;} function checkGoalReached (uint campaignID) public returns (bool reached) {Campaign storage c = campaigns [campaignID]; if (c.amount)

< c.fundingGoal) return false; uint amount = c.amount; c.amount = 0; c.beneficiary.transfer(amount); return true; }} 上面是一个简化版的众筹合约,但它可以让我们理解structs的基础概念,struct可以用于映射和数组中作为元素。其本身也可以包含映射和数组等类型。 不能声明一个struct同时将自身struct作为成员,这个限制是基于结构体的大小必须是有限的。 但struct可以作为mapping的值类型成员。 注意在函数中,将一个struct赋值给一个局部变量(默认是storage类型),实际是拷贝的引用,所以修改局部变量值的同时,会影响到原变量。 当然,也可以直接通过访问成员修改值,而不用一定赋值给一个局部变量,如campaigns[campaignID].amount = 0 映射(Mappings) 映射类型,一种键值对的映射关系存储结构。定义方式为mapping(_KeyType =>

_ KeyValue). Key types allow almost all types except mappings, variable-length arrays, contracts, enumerations, and structures (). There are no restrictions on value types and you can include mapping types for any type.

The mapping can be seen as a hash table, and all possible keys are virtualized and mapped to a default value of a type (binary all-zero representation). In the mapping table, the data for the key is not stored, only its keccak256 hash value, which is needed to find the value. Because of this, the mapping has no length, and there is no concept of a set of keys or values.

The mapping type can only be used as a state variable or as a reference to the storage type in an internal function.

You can have Solidity create an accessor by marking the mapping as public. Access it by providing a key value as a parameter, and the corresponding value is returned. The value type of the mapping can also be a mapping, and when accessed with an accessor, provide the key corresponding to the mapped value and repeat the process over and over again. Let's look at an example:

Pragma solidity ^ 0.4.0X contract MappingExample {mapping (address = > uint) public balances; function update (uint newBalance) public {balances [msg.sender] = newBalance;}} contract MappingUser {function f () public returns (uint) {MappingExample m = new MappingExample (); m.update (100); return m.balances (this);}}

Note: the mapping does not provide a method of iterative output, and you can implement such a data structure on your own.

At this point, the study of "what is the structure and mapping of Solidity" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report