Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

194题答案和个人思路(望大家指点) #5

Open
Fishicat opened this issue Apr 23, 2020 · 0 comments
Open

194题答案和个人思路(望大家指点) #5

Fishicat opened this issue Apr 23, 2020 · 0 comments

Comments

@Fishicat
Copy link

输出结果为[]

copy函数实际上会返回一个int值,这个int是一个size,计算逻辑为
size = min(len(dst), len(src))
这个size的大小,决定了src要copy几个元素给dst
由于题目中,dst声明了,但是没有进行初始化,所以dst的len是0
因此实际没有从src上copy到任何元素给dst

修改版本如下:

func main() {
        var src, dst []int
	src = []int{1, 2, 3}
        //这是未修改前的
	copy(dst, src)
	fmt.Println(dst)

        //这是修改时添加的
	dst = make([]int, len(src))
	copy(dst, src)
	fmt.Println(dst)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant