以数组类型为键的哈希表

huangapple go评论79阅读模式
英文:

Hash with key as an array type

问题

在Go语言中,可以使用数组作为map的键。以下是在Go中实现相同功能的示例代码:

package main

import "fmt"

func main() {
    quarters := map[[3]int]string{
        [3]int{1, 2, 3}:   "First quarter",
        [3]int{4, 5, 6}:   "Second quarter",
        [3]int{7, 8, 9}:   "Third quarter",
        [3]int{10, 11, 12}: "Fourth quarter",
    }

    fmt.Println(quarters[[3]int{1, 2, 3}])
    // 输出:"First quarter"
}

在这个示例中,我们使用[3]int作为数组的类型,表示键的类型是一个包含3个整数的数组。然后,我们创建一个quarters的map,将数组作为键,对应的字符串作为值。最后,我们可以通过指定相应的数组作为键来访问map中的值。

英文:

How to create a key as array in Go for map.
For example in ruby I can implement it such:

quarters = {
  [1, 2, 3] => 'First quarter',
  [4, 5, 6] => 'Second quarter',
  [7, 8 ,9] => 'Third quarter',
  [10, 11, 12] => 'Fourh quarter',
}
quarters[[1, 2, 3]] 
# => "First quarter"

How the same will be looked in Golang ?

答案1

得分: 8

Go中的数组类型(与切片不同)是可比较的,所以其中没有什么神奇的地方:你可以像定义其他映射一样定义它:map[KeyType]ValueType,其中KeyType将是[3]intValueType将是string

对于键类型的操作数,必须完全定义比较运算符==!=;因此,键类型不能是函数、映射或切片。

m := map[[3]int]string{}

m[[3]int{1, 2, 3}] = "First quarter"
m[[3]int{4, 5, 6}] = "Second quarter"
m[[3]int{7, 8, 9}] = "Third quarter"
m[[3]int{10, 11, 12}] = "Fourth quarter"

fmt.Println(m)

输出:

map[[1 2 3]:First quarter [4 5 6]:Second quarter 
    [7 8 9]:Third quarter [10 11 12]:Fourth quarter]

Go Playground上尝试一下。

要查询一个元素:

fmt.Println(m[[3]int{1, 2, 3}]) // 输出"First quarter"

你也可以一步创建映射:

m := map[[3]int]string{
    [3]int{1, 2, 3}:    "First quarter",
    [3]int{4, 5, 6}:    "Second quarter",
    [3]int{7, 8, 9}:    "Third quarter",
    [3]int{10, 11, 12}: "Fourth quarter",
}
英文:

Array types (unlike slices) in Go are comparable, so there is nothing magical in it: you can just define it like any other maps: map[KeyType]ValueType where KeyType will be [3]int and ValueType will be string.

> The comparison operators == and != must be fully defined for operands of the key type; thus the key type must not be a function, map, or slice.

m := map[[3]int]string{}

m[[3]int{1, 2, 3}] = "First quarter"
m[[3]int{4, 5, 6}] = "Second quarter"
m[[3]int{7, 8, 9}] = "Third quarter"
m[[3]int{10, 11, 12}] = "Fourth quarter"

fmt.Println(m)

Output:

map[[1 2 3]:First quarter [4 5 6]:Second quarter 
    [7 8 9]:Third quarter [10 11 12]:Fourth quarter]

Try it on the Go Playground.

To query an element:

fmt.Println(m[[3]int{1, 2, 3}]) // Prints "First quarter"

You can also create the map in one step:

m := map[[3]int]string{
	[3]int{1, 2, 3}:    "First quarter",
	[3]int{4, 5, 6}:    "Second quarter",
	[3]int{7, 8, 9}:    "Third quarter",
	[3]int{10, 11, 12}: "Fourth quarter",
}

答案2

得分: 1

需要注意的是,Ruby中的array和Go中的array是不同的数据结构。Go的数组是不可变的,而Ruby的array是可变的。Go的slice是更类似于Ruby的array的数据结构。然而,正如icza的回答所指出的那样,在Go中不能将slices用作键。

所以回答你的问题:

在Go中会是怎样的呢?

这是不可能的,在Go中你不能有一个将动态数组映射到字符串的字典。然而,你可以将slices转换为arrays并将它们用作键,icza已经很好地解释了这一点。

英文:

It should be noted that array in ruby and array in go are different data structures. Go array is not mutable, while ruby array is. Go slice is a lot more similar data structure to ruby array. However, you cannot use slices as keys in go, as pointed out in icza's answer.

So to answer your question:

> How the same will be looked in Golang ?

It is impossible, you cannot have a dictionary that maps dynamic arrays to strings in go. However, you could convert slices to arrays and use them as keys, as very well explained by icza.

huangapple
  • 本文由 发表于 2015年3月21日 04:25:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/29175247.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定