如何知道映射表中是否存在特定值?

Vis*_*tel 1 ethereum solidity smartcontracts

我有一个映射表,其中将多个哈希存储到该表中。我想做的是让用户使用setinstructors()函数添加另一个哈希,然后尝试查看映射表中是否已经存在相同的哈希。如果表中已经存在相同的哈希,则应返回true,否则返回false。这是我的代码:

pragma solidity ^0.4.18;

contract Hash{
bytes32 comphash;

struct hashstruct{
bytes32 fhash;

}
mapping (uint => hashstruct) hashstructs;
uint[] public hashAccts;



function setinstructor(uint _uint,string _fhash) public {
      var a = hashstructs[_uint];
   a.fhash = sha256(_fhash);  
     hashAccts.push(_uint) -1;


}



function getInstructor(uint ins) view public returns (bytes32) {
    return (hashstructs[ins].fhash);
}

   function count() view public returns (uint) {
    return hashAccts.length;
}



function setinstructors(string _comphash) public {
    comphash = sha256(_comphash);

}

function getInstructors() public constant returns (bytes32) {
    return (comphash);
}



}
Run Code Online (Sandbox Code Playgroud)

use*_*559 5

Solidity中没有“存在”之类的东西mapping

每个键都映射到某个东西。如果尚未设置任何值,则该值为0。

对于您的用例,hashstructs[n].fhash > 0可能就足够了。如果要明确显示,请在中添加bool existsstruct然后true在添加内容时将其设置为。然后使用hashstructs[n].exists检查是否存在。