solidity -> delete
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
pragma solidity ^0.8.9;
contract Delete {
// 01. string
string public str1 = "hello";
function deleteStr() public {
delete str1;
}
function setStr(string memory input) public {
str1 = input;
}
// 02. array 对于固定长度的数组,会删除每个元素的值,但是数组长度不变
uint256[10] public array1 = [1, 2, 3, 4, 5];
function deleteFiexedArray() public {
delete array1;
}
//03. array new
uint256[] array2;
function setArray2() public {
array2 = new uint256[](10);
for (uint256 i = 0; i < array2.length; i++) {
array2[i] = i;
}
}
function getArray2() public view returns (uint256[] memory) {
return array2;
}
function deleteArray2() public {
delete array2;
}
//04. mapping
mapping(uint256 => string) public m1;
function setMap() public {
m1[0] = "hello";
m1[1] = "solidity";
}
//Mapping不允许直接使用delete,但是可以对mapping的元素进行指定删除
function deleteMapping(uint256 i) public {
delete m1[i];
}
}
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!