//一个投票的合约
contract Ballot{
//提案对象
struct Proposal{
bytes32 name;
uint voteCount;//累计票数
}
//获取获胜的对象
function winningProposal() public view returns (Proposal[] memory)
{
Proposal[] memory win;
uint winningVoteCount = 0;
for(uint i=0;i < proposals.length;i++){
if(proposals[i].voteCount > winningVoteCount){
winningVoteCount = proposals[i].voteCount;
}
}
for(uint m=0; m < proposals.length; m++){
if(winningVoteCount == proposals[m].voteCount){
// push can't use
win.push(proposals[m]);
// next line can use
// win[win.length] = proposals[m];
}
}
return win;
}
}
Asked
Active
Viewed 19 times
1
lal
- 11
- 1
1 Answers
2
From https://docs.soliditylang.org/en/v0.8.16/types.html?highlight=Push#allocating-memory-arrays
As opposed to storage arrays, it is not possible to resize memory arrays (e.g. the .push member functions are not available).
mermeladeK
- 141
- 5
