OCy*_*ril 96 position go slice
如何确定切片中存在的元素的位置?
我需要以下内容:
type intSlice []int
func (slice intSlice) pos(value int) int {
for p, v := range slice {
if (v == value) {
return p
}
}
return -1
}
Run Code Online (Sandbox Code Playgroud)
Eva*_*haw 63
对不起,这里没有通用的库函数.Go没有直接的方法来编写可以在任何切片上运行的函数.
你的功能是有效的,但是如果你用它来写它会好一点range.
如果您碰巧有一个字节切片,则有bytes.IndexByte.
Ant*_*lin 48
你可以用惯用的方式创建泛型函数:
func SliceIndex(limit int, predicate func(i int) bool) int {
for i := 0; i < limit; i++ {
if predicate(i) {
return i
}
}
return -1
}
Run Code Online (Sandbox Code Playgroud)
用法:
xs := []int{2, 4, 6, 8}
ys := []string{"C", "B", "K", "A"}
fmt.Println(
SliceIndex(len(xs), func(i int) bool { return xs[i] == 5 }),
SliceIndex(len(xs), func(i int) bool { return xs[i] == 6 }),
SliceIndex(len(ys), func(i int) bool { return ys[i] == "Z" }),
SliceIndex(len(ys), func(i int) bool { return ys[i] == "A" }))
Run Code Online (Sandbox Code Playgroud)
Pod*_*.io 10
你可以写一个函数;
func indexOf(element string, data []string) (int) {
for k, v := range data {
if element == v {
return k
}
}
return -1 //not found.
}
Run Code Online (Sandbox Code Playgroud)
如果匹配元素,则返回字符/字符串的索引.如果未找到,则返回-1.
Go 从 1.18 版本开始支持泛型,它允许您创建像您这样的函数,如下所示:
func IndexOf[T comparable](collection []T, el T) int {
for i, x := range collection {
if x == el {
return i
}
}
return -1
}
Run Code Online (Sandbox Code Playgroud)
如果您希望能够调用IndexOf您的收藏,您也可以使用评论中的 @mh-cbon 技术。
从 Go 1.18 开始,您还可以使用https://pkg.go.dev/golang.org/x/exp/slices中的实验性通用切片包,如下所示:
package main
import "golang.org/x/exp/slices"
func main() {
s := []int{1,2,3,4,5}
wanted := 3
idx := slices.Index(s, wanted)
fmt.Printf("the index of %v is %v", wanted, idx)
}
Run Code Online (Sandbox Code Playgroud)
-1如果wanted不在切片中,它将返回。在操场上测试一下。
这是我更喜欢的方式,因为有一天它可能会成为标准库的一部分。
小智 6
在 Go 1.21 及更高版本中使用slices.Index。
haystack := []string{"foo", "bar", "quux"}
fmt.Println(slices.Index(haystack, "bar")) // prints 1
fmt.Println(slices.Index(haystack, "rsc")) // prints -1
Run Code Online (Sandbox Code Playgroud)
您可以迭代切片并检查元素是否与您选择的元素匹配。
func index(slice []string, item string) int {
for i := range slice {
if slice[i] == item {
return i
}
}
return -1
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
75007 次 |
| 最近记录: |