英文:
Unexpected behaviour when populating a map using recursion
问题
目标是计算从一组n个字符串中可以形成的所有长度为k的可能切片。
我尝试使用简单的递归来实现。只打印结果可以正常工作。将结果放入一个映射中,对于大于2的奇数k会产生一些意外的结果。
是什么导致了映射的键和它们对应的值之间的差异?
链接:https://play.golang.org/p/_SGrPRFjJ5g
package main
import (
"fmt"
"strings"
)
func main() {
QQM := make(map[string][]string)
states := []string{
"a",
"b",
}
var perm func(Q []string, k int)
perm = func(Q []string, k int) {
if k == 0 {
QQM[strings.Join(Q, "")] = Q
fmt.Println(QQM)
return
}
for i := 0; i < len(states); i++ {
perm(append(Q, states[i]), k-1)
}
}
perm([]string{}, 4)
}
map[aaaa:[a a a a]]
map[aaaa:[a a a b] aaab:[a a a b]]
map[aaaa:[a a a b] aaab:[a a a b] aaba:[a a b a]]
...
英文:
The goal is to compute all possible slices of length k that can be formed from a set of n strings.
I tried to do it using a simple recursion. Just printing the result works fine. Putting the results in a map yields some unexpected results for odd k's greater than 2.
What causes differences between the map's keys and their corresponding values?
https://play.golang.org/p/_SGrPRFjJ5g
package main
import (
"fmt"
"strings"
)
func main() {
QQM := make(map[string][]string)
states := []string{
"a",
"b",
}
var perm func(Q []string, k int)
perm = func(Q []string, k int) {
if k == 0 {
QQM[strings.Join(Q, "")] = Q
fmt.Println(QQM)
return
}
for i := 0; i < len(states); i++ {
perm(append(Q, states[i]), k-1)
}
}
perm([]string{}, 4)
}
map[aaaa:[a a a a]]
map[aaaa:[a a a b] aaab:[a a a b]]
map[aaaa:[a a a b] aaab:[a a a b] aaba:[a a b a]]
...
答案1
得分: 3
切片是对底层数组的一种视图。当你传递切片时,你传递的不是它们所包含的值,而是对这些值的引用。因此,当你将一个切片放入一个映射中,然后向该切片添加元素时,如果切片有足够的容量,你也会向映射中的切片添加元素。
在将切片放入映射之前,先进行复制:
newQ := make([]string, len(Q))
copy(newQ, Q)
QQM[strings.Join(Q, "")] = newQ
英文:
A slice is a view of an underlying array. When you pass slices around, you are not passing the values they contain, but references to those values. So when you put a slice to a map then add elements to that slice, if the slice has the capacity, you would be adding elements to the slice in the map as well.
Copy the slice before putting into the map:
newQ:=make([]string,len(Q))
copy(newQ,Q)
QQM[strings.Join(Q, "")] = newQ
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论