3. Solidity 기초 - Mapping

심재원·2023년 12월 20일
0

1. mapping(기본-1)

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.8.2 <0.9.0;

contract MAPPING {
    mapping(string => uint) age;
    mapping(string => bool) gender;

    function set_age(string memory _name, uint _age) public {
        age[_name] = _age; //MAPPING이름[key] = value
    }

    function get_age(string memory _name) public view returns(uint) {
        return age[_name];
    }

    function set_gender(string memory _name, bool _gender) public {
        gender[_name] = _gender;
    }

    function get_gender(string memory _name) public view returns(bool) {
        return gender[_name];
    }
}

2. mapping(기본-2, string ⇒ STRUCT)

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.8.2 <0.9.0;

contract MAPPING {
    struct Student {
        // string name;
        uint number;
        uint score;
        string home_address;
    }
    mapping(string => Student) students;

    function setStudent(string memory _name, uint _number, uint _score, string memory _home) public {
        students[_name] = Student(_number, _score, _home);
    }

    function getStudent(string memory _name) public view returns(Student memory) {
        return students[_name];
    }
}

3. mapping(기본-3, string⇒array)

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.8.2 <0.9.0;

contract MAPPING {
    struct Student {
        string name;
        uint number;
    }

    mapping(string => Student) students1;     // 담당 교사
    mapping(string => Student[]) students2;  // 반 담임 교사

    function setTeacherStudent1(string memory _teacher, Student memory _s1) public {
        students1[_teacher] = _s1;
    }

    function setTeacherStudent2(string memory _teacher, Student memory _s2) public {
        students2[_teacher].push(_s2);
    }

    function getStudent1(string memory _teacher) public view returns(Student memory) {
        return students1[_teacher];
    }

    function getStudents2(string memory _teacher) public view returns(Student[] memory) {
        return students2[_teacher];
    }
}

4. mapping(가스비 비교)

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.8.2 <0.9.0;

contract MAPPING {
    struct Student1 {
        // string name;
        uint number;
        uint score;
        string home_address;
    }

    struct Student2 {
        string name;
        uint number;
        uint score;
        string home_address;
    }

    mapping(string => Student1) s1;
    mapping(string => Student2) s2;

    function setStudent1(string memory _name, Student1 memory _s1) public {
        s1[_name] = _s1;
    }

    function setStudent2(string memory _name, Student2 memory _s2) public {
        s2[_name] = _s2;
    }
}

// s1 : 92142 , 69770
// s2 : 116059, 93219
// 결론 : 3개짜리가 더 싸다.

5. mapping(이중 매핑)

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.8.2 <0.9.0;

contract MAPPING {
    mapping(string => mapping(string=>uint)) accounts;

    function setAccount(string memory _name, string memory _bank, uint _amount) public {
        accounts[_name][_bank] = _amount;
    }

    function getAccount(string memory _name, string memory _bank) public view returns(uint) {
        return accounts[_name][_bank];
    }
}

0개의 댓글