英文:
How to maintain a set of integer sequences?
问题
set := make(map[string]bool)
set["Foo"] = true
foo_exists := set["Foo"]
如果我们想要一个整数序列的集合呢?Go编译器会报错Invalid key type []int
,因为make(map[[]int]bool)
是无效的。
集合是一种常用的基本数据类型,具有许多用途。例如,在密码生成/破解程序中,可以使用整数序列的集合来记录已生成/尝试的密码。这使得程序能够快速查找已生成/尝试的密码,以避免再次生成/尝试它们。这种数据结构非常基础,因此可以预期在各种应用程序中不时使用它。
英文:
To construct a set, we can use a map:
set := make(map[string]bool)
set["Foo"] = true
foo_exists := set["Foo"]
What if we want a set of integer sequences? Go compiler complains Invalid key type []int
for make(map[[]int]bool)
.
Set is a commonly used basic data type that has a lot of uses. For example, a set of integer sequences can be used in a password generating/cracking program to record passwords that have been generated/tried. This enables the program to quickly look up generated/tried passwords to avoid generating/trying them again. This data structure is simply so basic that it should be expected to be used from time to time within various kinds of applications.
答案1
得分: 2
根据@Wishwa Perera的回答,不可比较的类型不能用作映射键。因此,你可以将不同类型编码或转换为你的int slices
,然后插入到映射中。
在下面的示例中,我将int切片转换为字符串并插入到映射中。但是对于很长的int切片来说,这并不好,因为字符串会变得更长,运行代码所需的时间也会更长。
你可以使用哈希来解决这个问题。
你可以在这里运行代码。
package main
import (
"fmt"
)
type intArray []int
func (i intArray) toString() string{
return fmt.Sprintf(`%+v`, i)
}
func main() {
myMap := make(map[string]bool)
arr1 := []int{1, 2, 3, 4}
myMap[intArray(arr1).toString()] = true
arr2 := []int{1, 2, 3, 4}
arr2Exist := myMap[intArray(arr2).toString()]
fmt.Println(`is array 2 exist : `,arr2Exist) //is array 2 exist : true
arr3 := []int{1, 2, 3, 4, 5}
arr3Exist := myMap[intArray(arr3).toString()]
fmt.Println(`is array 3 exist : `,arr3Exist) //is array 3 exist : false
}
英文:
As explained in @Wishwa Perera's answer, types which are not comparable are can not use as map key. So you can encode, or convert different type to your int slices
and then insert to the map.
In below example, I convert int slice to string and insert to the map. But this is not good for long int slices because string is become more longer and more time consumed to run the code.
You can use hashing to solve that problem.
package main
import (
"fmt"
)
type intArray []int
func (i intArray) toString() string{
return fmt.Sprintf(`%+v`, i)
}
func main() {
myMap := make(map[string]bool)
arr1 := []int{1, 2, 3, 4}
myMap[intArray(arr1).toString()] = true
arr2 := []int{1, 2, 3, 4}
arr2Exist := myMap[intArray(arr2).toString()]
fmt.Println(`is array 2 exist : `,arr2Exist) //is array 2 exist : true
arr3 := []int{1, 2, 3, 4, 5}
arr3Exist := myMap[intArray(arr3).toString()]
fmt.Println(`is array 3 exist : `,arr3Exist) //is array 3 exist : false
}
You can run here
答案2
得分: 1
地图键可以是任何可比较的类型。其中一些类型包括:
- 布尔类型
- 数值类型
- 字符串类型
- 指针类型
- 通道类型
- 接口类型
- 结构体类型 - 如果所有字段类型都是可比较的
- 数组类型 - 如果数组元素的类型是可比较的
一些不可比较的类型不能用作地图的键:
- 切片类型
- 地图类型
- 函数类型
你使用的是一个切片,应该使用数组代替,定义一个大小(比如说5)为make(map[[5]int]bool)
,这样就可以编译通过了。
英文:
The map key can be any type that is comparable. Some of them are:
- Boolean
- Numeric
- String
- Pointer
- Channel
- Interface types
- Structs — if all it’s field type is comparable
- Array – if the type of value of array element is comparable
Some types which are not comparable and which cannot be used as a key in a map are:
- Slice
- Map
- Function
What you have used is a slice,use an array instead, define a size (let say 5) as make(map[[5]int]bool)
and it will compile.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论