首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将整数切片追加到整数切片的切片会修改要追加的切片

将整数切片追加到整数切片的切片会修改要追加的切片
EN

Stack Overflow用户
提问于 2016-06-19 01:14:58
回答 1查看 233关注 0票数 3

我正在尝试将一个整数切片附加到由整数切片组成的切片上。当我打印切片时,它会像预期的那样出现。但是,当我将切片附加到切片上时,内容会发生变化。

代码语言:javascript
运行
复制
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]]

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

EN

回答 1

Stack Overflow用户

发布于 2016-06-19 02:15:33

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

代码语言:javascript
运行
复制
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)
}

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37899640

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档