gpt4 book ai didi

go - 将整数 slice append 到整数 slice 的 slice 会修改被 append 的 slice

转载 作者:IT王子 更新时间:2023-10-29 01:45:14 28 4
gpt4 key购买 nike

我正在尝试将整数 slice append 到由整数 slice 组成的 slice 。当我打印 slice 时,它按预期显示。但是,当我将 slice append 到一片 slice 时,内容会发生变化。

package main

import "fmt"

var myGraph [8][8]bool //the graph

var visited [8]bool //an array that marks if visited

var path []int //a slice to store a possible path

var paths [][]int

func dfs(src int, dest int) {
//add current node to path
path = append(path, src)

//mark current node as visited
visited[src] = true

//if the current node is the destination
//print the path and return
if src == dest {
fmt.Println(path)
paths = append(paths, path) //I'm trying to push the path slice into the paths slice
return
}

for i := 1; i <= 7; i++ { //loop through all nodes

//if ith node is a neighbour of the current node and it is not visited
if myGraph[src][i] && visited[i] == false {

// call dfs on the current node
dfs(i, dest)

//mark the current node as unvisited
//so that we can other paths to the final destination
visited[i] = false

//re-slice the slice - get rid of the current node
path = path[:len(path)-1]
}

}

}

func main() {
path = make([]int, 0, 8)
paths = make([][]int, 0, 10)

//creating the graph
myGraph[1] = [...]bool{false, false, true, true, false, false, true, false}
myGraph[2] = [...]bool{false, true, false, true, false, true, false, false}
myGraph[3] = [...]bool{false, true, true, false, true, false, true, false}
myGraph[4] = [...]bool{false, false, false, true, false, false, true, false}
myGraph[5] = [...]bool{false, false, true, false, false, false, true, false}
myGraph[6] = [...]bool{false, true, false, true, true, false, false, true}
myGraph[7] = [...]bool{false, false, false, false, false, false, true, false}

//call dfs by feeding in the source and the destination
dfs(1, 7)
fmt.Println(paths)
}

Output:
[1 2 5 6 7]
[1 3 2 5 6 7]
[1 3 4 6 7]
[1 3 6 7]
[1 6 7]
[[1 6 7 3 2 5] [1 6 7 3 2] [1 6 7 3 2] [1 6 7 3 2 5] [1 6 7 3 2] [1 6 7 3] [1 6 7]]

如您所见, slice 的 slice 由 main 函数在末尾打印。但是,它的内容与 dfs() 打印的各个切​​片不同。

最佳答案

让我们稍微修改一下这段代码:

var myGraph [8][8]bool //the graph

var visited [8]bool //an array that marks if visited

var path []int //a slice to store a possible path

var paths [][]int

func dfs(src int, dest int) {
//add current node to path
path = append(path, src)

//mark current node as visited
visited[src] = true

//if the current node is the destination
//print the path and return
if src == dest {
// (A)
buffer := make([]int, len(path))
copy(buffer, path)

fmt.Println(buffer)
paths = append(paths, buffer) //I'm trying to push the path slice into the paths slice

// fmt.Println(path)
// paths = append(paths, path) //I'm trying to push the path slice into the paths slice
return
}

for i := 1; i <= 7; i++ { //loop through all nodes

//if ith node is a neighbour of the current node and it is not visited
if myGraph[src][i] && visited[i] == false {

// call dfs on the current node
dfs(i, dest)

//mark the current node as unvisited
//so that we can other paths to the final destination
visited[i] = false

//re-slice the slice - get rid of the current node
path = path[:len(path)-1]
}

}

}

func main1() {
// path = make([]int, 0, 8)
// paths = make([][]int, 0, 10)

//creating the graph
myGraph[1] = [...]bool{false, false, true, true, false, false, true, false}
myGraph[2] = [...]bool{false, true, false, true, false, true, false, false}
myGraph[3] = [...]bool{false, true, true, false, true, false, true, false}
myGraph[4] = [...]bool{false, false, false, true, false, false, true, false}
myGraph[5] = [...]bool{false, false, true, false, false, false, true, false}
myGraph[6] = [...]bool{false, true, false, true, true, false, false, true}
myGraph[7] = [...]bool{false, false, false, false, false, false, true, false}

//call dfs by feeding in the source and the destination
dfs(1, 7)
fmt.Println(paths)
}

现在它按预期工作了!为什么?因为 slice 是引用类型,不是值类型;意味着当您稍后更改 path 时,您将看到该新值,除非您像在 //(A) 中那样对其进行快照。

关于go - 将整数 slice append 到整数 slice 的 slice 会修改被 append 的 slice ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37899640/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com