Solidity – 映射
Solidity 中的映射就像任何其他语言的哈希表或字典。这些用于以键值对的形式存储数据,键可以是任何内置数据类型,但不允许引用类型,而值可以是任何类型。映射主要用于将唯一的以太坊地址与关联的值类型相关联。
句法:
mapping(key => value) ;
创建映射
映射被定义为任何其他变量类型,它接受键类型和值类型。
示例:在下面的示例中,合约 mapping_example定义了一个结构并创建了映射。
Solidity
// Solidity program to
// demonstrate mapping
pragma solidity ^0.4.18;
// Defining contract
contract mapping_example {
//Defining structure
struct student
{
// Declaring different
// structure elements
string name;
string subject;
uint8 marks;
}
// Creating a mapping
mapping (
address => student) result;
address[] public student_result;
}
Solidity
// Solidity program to
// demonstrate adding
// values to mapping
pragma solidity ^0.4.18;
// Creating contract
contract mapping_example {
//Defining structure
struct student {
//Declaring different
// structure elements
string name;
string subject;
uint8 marks;
}
// Creating mapping
mapping (
address => student) result;
address[] public student_result;
// Function adding values to
// the mapping
function adding_values() public {
var student
= result[0xDEE7796E89C82C36BAdd1375076f39D69FafE252];
student.name = "John";
student.subject = "Chemistry";
student.marks = 88;
student_result.push(
0xDEE7796E89C82C36BAdd1375076f39D69FafE252) -1;
}
}
Solidity
// Solidity program to
// demonstrate retrieve
// values from the mapping
pragma solidity ^0.4.18;
contract mapping_example {
// Defining Structure
struct student {
// Declaring different data types
string name;
string subject;
uint8 marks;
}
// Creating mapping
mapping (
address => student) result;
address[] student_result;
// Function adding values to the mapping
function adding_values() public {
var student
= result[0xDEE7796E89C82C36BAdd1375076f39D69FafE252];
student.name = "John";
student.subject = "Chemistry";
student.marks = 88;
student_result.push(
0xDEE7796E89C82C36BAdd1375076f39D69FafE252) -1;
}
// Function to retrieve
// values from a mapping
function get_student_result(
) view public returns (address[]) {
return student_result;
}
}
Solidity
// Solidity program to
// count number of
// values in a mapping
pragma solidity ^0.4.18;
contract mapping_example {
// Defining structure
struct student {
// Declaring different
// structure elements
string name;
string subject;
uint8 marks;
}
// Creating mapping
mapping (address => student) result;
address[] student_result;
//Function adding values to the mapping
function adding_values() public {
var student
= result[0xDEE7796E89C82C36BAdd1375076f39D69FafE252];
student.name = "John";
student.subject = "Chemistry";
student.marks = 88;
student_result.push(
0xDEE7796E89C82C36BAdd1375076f39D69FafE252) -1;
}
// Function to retrieve
// values from the mapping
function get_student_result(
) view public returns (address[]) {
return student_result;
}
// Function to count number
// of values in a mapping
function count_students(
) view public returns (uint) {
return student_result.length;
}
}
输出 :
向映射添加值
创建映射后,让我们尝试为映射添加一些值以便更好地理解。
示例:在下面的示例中,合约 mapping_example定义了一个结构,创建了映射并将值添加到映射中。
坚固性
// Solidity program to
// demonstrate adding
// values to mapping
pragma solidity ^0.4.18;
// Creating contract
contract mapping_example {
//Defining structure
struct student {
//Declaring different
// structure elements
string name;
string subject;
uint8 marks;
}
// Creating mapping
mapping (
address => student) result;
address[] public student_result;
// Function adding values to
// the mapping
function adding_values() public {
var student
= result[0xDEE7796E89C82C36BAdd1375076f39D69FafE252];
student.name = "John";
student.subject = "Chemistry";
student.marks = 88;
student_result.push(
0xDEE7796E89C82C36BAdd1375076f39D69FafE252) -1;
}
}
输出 :
获取值
我们已经向映射添加了值,为了检索我们必须创建一个函数来返回添加到映射中的值的值。
示例:在下面的示例中,合约 mapping_example定义了一个结构,创建了映射,将值添加到映射中,并从映射中检索值。
坚固性
// Solidity program to
// demonstrate retrieve
// values from the mapping
pragma solidity ^0.4.18;
contract mapping_example {
// Defining Structure
struct student {
// Declaring different data types
string name;
string subject;
uint8 marks;
}
// Creating mapping
mapping (
address => student) result;
address[] student_result;
// Function adding values to the mapping
function adding_values() public {
var student
= result[0xDEE7796E89C82C36BAdd1375076f39D69FafE252];
student.name = "John";
student.subject = "Chemistry";
student.marks = 88;
student_result.push(
0xDEE7796E89C82C36BAdd1375076f39D69FafE252) -1;
}
// Function to retrieve
// values from a mapping
function get_student_result(
) view public returns (address[]) {
return student_result;
}
}
输出 :
计数映射
可以对映射进行计数,以便我们可以知道映射中存储了多少值。
示例:在下面的示例中,合约 mapping_example定义了一个结构,创建了映射,将值添加到映射中,并从映射中检索值。
坚固性
// Solidity program to
// count number of
// values in a mapping
pragma solidity ^0.4.18;
contract mapping_example {
// Defining structure
struct student {
// Declaring different
// structure elements
string name;
string subject;
uint8 marks;
}
// Creating mapping
mapping (address => student) result;
address[] student_result;
//Function adding values to the mapping
function adding_values() public {
var student
= result[0xDEE7796E89C82C36BAdd1375076f39D69FafE252];
student.name = "John";
student.subject = "Chemistry";
student.marks = 88;
student_result.push(
0xDEE7796E89C82C36BAdd1375076f39D69FafE252) -1;
}
// Function to retrieve
// values from the mapping
function get_student_result(
) view public returns (address[]) {
return student_result;
}
// Function to count number
// of values in a mapping
function count_students(
) view public returns (uint) {
return student_result.length;
}
}
输出 :