duanlinpi0265 2011-01-16 20:18
浏览 28
已采纳

使用具有用户定义类型的地图的设置属性

I'm trying to use the built-in map type as a set for a type of my own (Point, in this case). The problem is, when I assign a Point to the map, and then later create a new, but equal point and use it as a key, the map behaves as though that key is not in the map. Is this not possible to do?

// maptest.go

package main

import "fmt"

func main() {
    set := make(map[*Point]bool)

    printSet(set)
    set[NewPoint(0, 0)] = true
    printSet(set)
    set[NewPoint(0, 2)] = true
    printSet(set)

    _, ok := set[NewPoint(3, 3)] // not in map
    if !ok {
        fmt.Print("correct error code for non existent element
")
    } else {
        fmt.Print("incorrect error code for non existent element
")
    }

    c, ok := set[NewPoint(0, 2)] // another one just like it already in map
    if ok {
        fmt.Print("correct error code for existent element
") // should get this
    } else {
        fmt.Print("incorrect error code for existent element
") // get this
    }

    fmt.Printf("c: %t
", c)
}

func printSet(stuff map[*Point]bool) {
    fmt.Print("Set:
")
    for k, v := range stuff {
        fmt.Printf("%s: %t
", k, v)
    }
}

type Point struct {
    row int
    col int
}

func NewPoint(r, c int) *Point {
    return &Point{r, c}
}

func (p *Point) String() string {
    return fmt.Sprintf("{%d, %d}", p.row, p.col)
}

func (p *Point) Eq(o *Point) bool {
    return p.row == o.row && p.col == o.col
}
  • 写回答

1条回答 默认 最新

  • dongnaopa6200 2011-01-16 21:31
    关注
    package main
    
    import "fmt"
    
    type Point struct {
        row int
        col int
    }
    
    func main() {
        p1 := &Point{1, 2}
        p2 := &Point{1, 2}
        fmt.Printf("p1: %p %v p2: %p %v
    ", p1, *p1, p2, *p2)
    
        s := make(map[*Point]bool)
        s[p1] = true
        s[p2] = true
        fmt.Println("s:", s)
    
        t := make(map[int64]*Point)
    t[int64(p1.row)<<32+int64(p1.col)] = p1
    t[int64(p2.row)<<32+int64(p2.col)] = p2
        fmt.Println("t:", t)
    }
    
    Output:
    p1: 0x7fc1def5e040 {1 2} p2: 0x7fc1def5e0f8 {1 2}
    s: map[0x7fc1def5e0f8:true 0x7fc1def5e040:true]
    t: map[4294967298:0x7fc1def5e0f8]
    

    If we create pointers to two Points p1 and p2 with the same coordinates they point to different addresses.

    s := make(map[*Point]bool) creates a map where the key is a pointer to the memory allocated to a Point and the value is boolean value. Therefore, if we assign elements p1 and p2 to the map s then we have two distinct map keys and two distinct map elements with the same coordinates.

    t := make(map[int64]*Point) creates a map where the key is a composite of the coordinates of a Point and the value is a pointer to the Point coordinates. Therefore, if we assign elements p1 and p2 to the map t then we have two equal map keys and one map element with the shared coordinates.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法