Solidity学习过程:mapping映射

  • Post author:
  • Post category:solidity


pragma solidity^0.5.0;

contract mappingTest{
    //idmapping代表地址与id映射到了一起,namemapping代表id与名字的字符串映射到了一起
    
    mapping(address => uint) idmapping;//0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2 => 1
    mapping(uint => string) namemapping;//1 =>tck 
    
    uint sum = 0;
    function register(string memory name) public {
        address account = msg.sender;
        sum++;
        //将合约调用者的地址与注册总量id联系到一起
        idmapping[account] = sum;
        //当前用户id与用户注册的名字绑定到了一起
        namemapping[sum] = name;
    }
    //通过关键字地址获取到和他绑定到一起的id值
    function getidByaddress(address are) public view returns(uint){
        return idmapping[are];
    }

    //通过id值获取到和他绑定在一起的名字
    function getnameByid(uint  id) public view returns(string memory){
        return namemapping[id];
    }
    
}



版权声明:本文为m0_46328542原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。