solidity mapping of mapping,两层映射,用的时候可以像二维数组一样去访问和修改值,非常方便。

以下代码示例中的这一句:

mapping(string => mapping(uint => uint)) prices

相当于建立了一个price数据库表(只不过存在内存中),表结构为:index, date, price, key值为index + date。

pragma solidity ^0.4.21;

contract mappingOfMapping{
    
    mapping(string => mapping(uint => uint)) prices; // string代表指数名,一个uint代表收盘日期,第二个uint代表收盘价格
    
    function setPrice(string _index, uint _date, uint _price) public{ // 设置某指数某天的收盘价
        prices[_index][_date] = _price; 
    }
    
    function getPrice(string _index,uint _date) public view returns(uint){ // 获取某指数某天的收盘价
        return(prices[_index][_date]);
    }
    
}

 

相关文章:

  • 2022-12-23
  • 2022-01-25
  • 2022-12-23
  • 2021-12-21
  • 2021-07-26
  • 2021-05-22
猜你喜欢
  • 2022-12-23
  • 2021-12-09
  • 2021-08-08
  • 2022-12-23
  • 2022-12-23
  • 2022-01-17
  • 2022-03-02
相关资源
相似解决方案