7. 一般集聚 (Common Collections)
useful data structures called collections. Most other data types represent one specific value, but collections can contain multiple values. Unlike the built-in array and tuple types which are allocated on the "stack" and cannot grow in size, the data these collections point to is stored either on the "heap" or in contract "storage", which means the amount of data does not need to be known at compile time and can grow as the program runs. Each kind of collection has different capabilities and costs, and choosing an appropriate one for your current situation is a skill you’ll develop over time. In this chapter, we’ll discuss three collections that are used very often in Sway programs:
集聚就是有用数据结构。大多数其他数据类型表示一个特定值,但集聚可以包含多个值。与内置的数组和元组类型分配在“堆栈”上并且大小不能增长不同,这些集聚指向的数据存储在“堆”或合约“存储”中,这意味着数据量不需要在编译时知晓并且可以随着程序运行而增长。每种集聚都有不同的功能和成本,选择适合您当前情况的集聚,是一项您会随着时间的推移而培养的技能。在本章中,我们将讨论 Sway 程序中经常使用的三个集聚:
A vector on the heap allows you to store a variable number of values next to each other.
堆上的向量允许您将相邻的可变数量的值存储。
A storage vector is similar to a vector on the heap but uses persistent storage.
存储向量类似于堆上的向量,但使用持久存储。
A storage map allows you to associate a value with a particular key.
存储映射允许您将值与特定键相关联。
We’ll discuss how to create and update vectors, storage vectors, and storage maps, as well as what makes each special.
我们将讨论如何创建和更新向量、存储向量和存储映射,以及它们的特殊之处。
Last updated