在 solidity 中使用 "mapping" 的好处或主要原因是什么?

What is the benefit or the main reason of using "mapping" in solidity?

我是 solidity 的初学者。我正在学习扎实的映射。我对为什么要使用映射感到困惑?

例如,这是我看到的映射示例:

contract TestContract {
    struct Buyer {
        bytes32 name;
        uint price;
    }
    mapping(address => Buyer) buyerInfo;
}

似乎试图使用地址来获取结构中定义的买家信息。但是如果我在struct中创建一个新的地址类型成员呢,那么似乎就不需要使用映射的概念了。

contract TestContract2 {
    struct Buyer {
        address addr;
        bytes32 name;
        uint price;
    }
}

谁能解释一下使用映射的好处或主要原因是什么?

contract TestContract {
    struct Buyer {
        bytes32 name;
        uint price;
    }

    mapping(address => Buyer) buyerInfo;
    Buyerp[] buyerInfoList; // array

    function testFunc() {
        // Map: we can access easily
        Buyer bb = buyerInfo[msg.sender];

        // Array: we need for loop to access it
        for(uint i = 0;i<buyerInfoList.length;i++) {
            if(buyerInfoList[i].address == msg.sender) {
                Buyer bb = buyerInfo[i];
            }
        }
        
        
    }
}

并且,在 solidity 中,map 的数据是私有的,因此,没有密钥,任何人都无法扩展其数据。

而且,如果我们知道 key,我们可以轻松地访问它的值(一行)。