如何在 Go 中实现可调整大小的数组
我有 C++ 背景,习惯于使用 std::vector
类来完成类似的事情。 假设我想要一个动态数组:
type a struct {
b int
c string
}
执行此操作的标准方法是什么?
代码片段会非常有用
I come from a C++ background and I'm used to using the std::vector
class for things like this.
Lets assume I want a dynamic array of these:
type a struct {
b int
c string
}
What is the standard way of doing this?
A snippet would be very useful
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用
append()
内置示例:
请参阅规范了解更多信息附加信息。
Use the
append()
builtinExample:
Refer to the spec for more info on append.
Go Slice 包含三个元素:数据、长度和容量。
变量 s 是一个长度为 0、容量为 10 的整数切片。内置的 len() 和 cap() 函数允许您获取切片的长度和容量:
增加切片的长度,简单地重新切片:
要减少长度,您可以采用子切片:
有一些更短的方法来调用 make():
这一切都很好,但是如果您需要增加切片的长度超出其长度怎么办?容量?为此,您需要分配一个新切片并将旧切片的内容复制到新切片。 (“copy”函数是另一个内置函数。)
Effective Go 文档 采用了这个再进一步举例,实现一个 Append 函数,将一个切片附加到另一个切片,并在必要时调整其大小。
切片由数组支持;当您 make() 特定容量的切片时,会在后台分配该容量的数组。该切片实际上成为该数组的“智能指针”。如果将该切片(或该切片的子切片)传递给另一个函数,它将作为指向同一数组的指针传递。这使得子切片的创建成本非常低——昂贵的是支持数组的分配。
Go 标准库包含许多容器包(例如向量),无需手动管理切片。使用切片来提高速度,使用更复杂的容器类来提高便利性。 (话虽如此,我仍然在大多数事情上使用切片。)
您可能想知道为什么需要这么麻烦。毕竟,许多语言都提供动态调整大小的数组作为原语。其原因与 Go 的哲学有关。语言设计者并不假定知道适合您的程序的分配策略是什么;相反,它们为您提供了构建自己的数据结构所需的工具。
A Go Slice contains three elements: data, length, and capacity.
The variable s is a slice of ints with a length of 0 and a capacity of 10. The built-in len() and cap() functions allow you to get the length and capacity of a slice:
To increase the length of a slice, simply re-slice:
To decrease the length, you can take a sub-slice:
There are some shorter ways to invoke make():
That's all well and good, but what if you need to increase the length of a slice beyond its capacity? To do that, you need to allocate a new slice and copy the contents of the old slice to the new one. (The function "copy" is another built-in.)
The Effective Go document takes this example a bit further, implementing an Append function that appends one slice to another, resizing it if necessary.
Slices are backed by arrays; when you make() a slice of a specific capacity, an array of that capacity is allocated in the background. The slice effectively becomes a "smart pointer" to that array. If you pass that slice (or a subslice of that slice) to another function, it is passed as a pointer to that same array. This makes sub-slices very cheap to create - it's the allocation of the backing array that is expensive.
The Go standard library includes a number of container packages - vector, for instance - that eliminate the need to manually manage slices. Use slices for speed, and more elaborate container classes for convenience. (Saying that, I still use slices for most things.)
You may be wondering why you need to go to all this trouble. After all, a lot of languages provide dynamically resized arrays as primitives. The reason for this is tied to Go's philosophy. The language designers don't presume to know what the appropriate allocation policy is for your program; instead they give you the tools you need to build your own data structures.
执行此操作的惯用方式已经改变。
添加内置的append()函数意味着您可以像这样扩展切片:
如果有空间,Append()会将给定的项目追加到切片中;如果切片不够大,则扩展切片。
有关append()的更多信息请参见http://golang.org/doc/go_spec.html#Appending_and_copying_slices
The idiomatic way to do this has changed.
The addition of the built-in append() function means that you can extend a slice like so:
Append() will append the given item to the slice if there is room or extend the slice if it's not bigger enough.
More information about append() is here http://golang.org/doc/go_spec.html#Appending_and_copying_slices
的简单示例
有关
append()
内置< code>append() 文档在这里For a Simpler Example of the
append()
builtinappend()
Documentation here你也许也可以吃一片。这是一个知道其当前长度的数组。并可以有单独的电流长度和最大容量。请注意,为初始大小和容量传递的值不必是常量,因此您可以创建一个函数,该函数根据其参数构建并返回不同长度的切片。
好处是切片 []Int 可以像数组一样进行索引,并且以这种方式使用时将返回整数。
缺点是它不会自动增长超过其规定的容量。 Effective Go 有一个示例,说明如何处理重新分配。
代码是
you might also be able to make do with a slice. which is an array that knows its current length. And can have a separate current length and maximum capacity. Note the values passed for initial size and capacity do not have to be constants so you can create a function which builds and returns slices of different lengths based on its parameters.
The up side is that a slice []Int can just be indexed like an array, and will return ints when used in this way.
The downside is that it will not automatically grow byound its stated capacity. Effective Go has an example of how you would go about handling reallocation.
the code would be
您好,我们可以通过两种方式简单地做到这一点
就像这样
__
__
添加您想要的数量。第一个是执行此操作的简单方法。希望这会对您有所帮助。
Hi we can simply do this in two ways
Just do like this
__
__
Add as much as you want. First one is an easy way to do this. Hope this will help you.
如果你想在 Go 中拥有一个动态列表,那么你必须使用 Slice
你可以在这里了解更多信息:
https://blog.golang.org/slices-intro
一些示例:
https://tour.golang.org/moretypes/7
https://gobyexample.com/slices
If you want to have a dynamic List in Go then you have to use Slice
you can learn more about it here:
https://blog.golang.org/slices-intro
Some Examples:
https://tour.golang.org/moretypes/7
https://gobyexample.com/slices