英文:
convert array from int8 to int in go
问题
我有一个调用函数的问题
sort.SearchInts(arrInt, 10)
因为数组(arrInt)的类型是int8,我需要将数组转换为int类型。
我猜我可以通过使用for循环在数组中搜索元素,但这样做听起来很丑陋,我想可能有更好的方法来解决这个问题。
英文:
I have a problem to call function
sort.SearchInts(arrInt, 10)
because array (arrInt) has a type int8 and I need convert array to type int.
I guess I could go the long way and search an element in a for loop, but it sounds ugly and I guess there are better ways to do it.
答案1
得分: 2
数组是内存中大小相同的连续项的序列:
int8数组:[1字节][1字节][1字节][1字节][1字节]...
如果我们需要int类型的数组(在amd64中通常为8字节),我们有:
int数组:[8字节][8字节][8字节][8字节][8字节]...
没有简单地从一种类型转换或转换为另一种类型的方法,获取int数组的唯一方法是创建一个新的int数组,并将所有int8值转换为int。
另一方面,如果你只需要在数组中搜索,进行所有的转换是不必要的,最好的方法是创建一个名为SearchInt8
的函数。
这就是泛型很有用的地方,相同操作的类型可以应用相同的算法,但是Golang缺乏泛型。为了得到这个结果,Golang提供了sort.Search
函数(https://golang.org/pkg/sort/#Search),它接受第二个参数作为实现比较的函数。
提醒:要使用Search函数,数组应该已经排序好。
英文:
An array is a contiguous sequence of items of the same size in memory:
Array of int8: [1 byte][1 byte][1 byte][1 byte][1 byte]...
If we need array of ints (tipically 8 bytes in amd64) we have:
Array of int: [8 bytes][8 bytes][8 bytes][8 bytes][8 bytes]...
There is no way to simply convert or cast from one type to the other, the only way to get the array of ints is creating a new array of int and converting all values from int8 to int.
On the other hand, if you only need to search in an array, doing all the conversion is overkill, the best approach is creating a function SearchInt8
.
Here is where generics are useful, the same algorithm will be aplicable to types with the same operations but Golang lack of generics. To get this result, golang comes with the function sort.Search
(https://golang.org/pkg/sort/#Search) that accepts as second argument the function to implement the comparation for your type.
Reminder: to use Search functions, the array should be already sorted.
答案2
得分: 1
sort.SearchInts
只是对int
切片的sort.Search
的一个方便包装。
它的实现如下:
func SearchInts(a []int, x int) int {
return Search(len(a), func(i int) bool { return a[i] >= x })
}
因此,你可以创建一个自定义的"SearchInt8s
"函数,用于处理int8
参数:
func SearchInt8s(a []int8, x int8) int {
return sort.Search(len(a), func(i int) bool { return a[i] >= x })
}
或者(正如另一个答案已经提到的),直接使用sort.Search
:
sort.Search(len(arrInt), func(i int) bool { return arrInt[i] >= 10 })
请注意,在调用这些函数之前,切片必须是已排序的,因为这些函数在已排序的切片中执行二分查找。
英文:
sort.SearchInts
is just a convenience wrapper of sort.Search
for slices of int
s.
It's implemented like this:
func SearchInts(a []int, x int) int {
return Search(len(a), func(i int) bool { return a[i] >= x })
}
So you can create a custom "SearchInt8s
" function that does the same for int8
arguments:
func SearchInt8s(a []int8, x int8) int {
return sort.Search(len(a), func(i int) bool { return a[i] >= x })
}
Or (as another answer already said), just use sort.Search
directly:
sort.Search(len(arrInt), func(i int) bool { return arrInt[i] >= 10 })
Do note that the slice must be sorted before calling this, since this family of functions perform a binary search in a sorted slice.
答案3
得分: 0
sort.Search(len(arrInt), func(i int) bool { return arrInt[i] >= 10 })
英文:
sort.Search(len(arrInt), func(i int) bool { return arrInt[i] >= 10 })
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论