英文:
Is it possible for a function to change the size of a slice of string array declared elsewhere? golang
问题
我想要在切片中就地删除一个元素。但是当我这样做时,会在底层数组中产生两个空元素。
我已经在这里和这里进行了搜索。
package main
import "fmt"
//String remove adjacent duplicates from a string array
func rmDup(str []string) []string {
for i := 1; i < len(str); i++ {
if str[i] == str[i-1] {
copy(str[i:], str[i+1:])
str[len(str)-1] = ""
str = str[:len(str)-1]
}
}
return str
}
func main() {
str := []string{"dog", "cat", "cat", "mouse", "mouse", "zebra", "zebra"}
fmt.Println(str)
newstr := rmDup(str)
fmt.Println(str)
fmt.Println(newstr)
fmt.Println(len(str), cap(str), "final")
fmt.Println(len(newstr), cap(newstr), "final")
}
在main
函数中,有没有办法让str
返回rmDup
函数中定义的size
和capacity
呢?
英文:
I would like to remove an element from the slice in place. But when I do this I end up producing two empty elements in the underlying array.
I already searched here, here
package main
import "fmt"
//String remove adjacent duplicates from a string array
func rmDup(str []string) []string {
for i := 1; i < len(str); i++ {
if str[i] == str[i-1] {
copy(str[i:], str[i+1:])
str[len(str)-1] = ""
str = str[:len(str)-1]
}
}
return str
}
func main() {
str := []string{"dog", "cat", "cat", "mouse", "mouse", "zebra", "zebra"}
fmt.Println(str)
newstr := rmDup(str)
fmt.Println(str)
fmt.Println(newstr)
fmt.Println(len(str), cap(str), "final")
fmt.Println(len(newstr), cap(newstr), "final")
}
Is there any way that str in main can return the size and capacity defined in rmDup()
1: https://github.com/golang/go/wiki/SliceTricks
2: https://blog.golang.org/go-slices-usage-and-internals
答案1
得分: 3
原来我能够自己找到答案。由于Go语言通过值进行函数调用,所以无法在另一个作用域中更改已声明的切片,除非使用指针。
package main
import "fmt"
//String remove adjacent duplicates from a string array
func rmDup(str *[]string) []string {
var s = *str
for i := 1; i < len(s); i++ {
if s[i] == s[i-1] {
copy(s[i:], s[i+1:])
s[len(s)-1] = ""
s = s[:len(s)-1]
}
}
*str = s
return s
}
func main() {
str := []string{"dog", "cat", "cat", "mouse", "mouse", "zebra",
"zebra"}
fmt.Println(str)
newstr := rmDup(&str)
fmt.Println(str)
fmt.Println(newstr)
fmt.Println(len(str), cap(str), "final")
fmt.Println(len(newstr), cap(newstr), "final")
}
输出:
[dog cat cat mouse mouse zebra zebra]
[dog cat mouse zebra]
[dog cat mouse zebra]
4 7 final
4 7 final
英文:
It turned out that I was able to find the answer myself. Since the Go language performs function calls by value it is impossible to change a slice declared in another scope, except using pointers.
package main
import "fmt"
//String remove adjacent duplicates from a string array
func rmDup(str *[]string) []string {
var s = *str
for i := 1; i < len(s); i++ {
if s[i] == s[i-1] {
copy(s[i:], s[i+1:])
s[len(s)-1] = ""
s = s[:len(s)-1]
}
}
*str = s
return s
}
func main() {
str := []string{"dog", "cat", "cat", "mouse", "mouse", "zebra",
"zebra"}
fmt.Println(str)
newstr := rmDup(&str)
fmt.Println(str)
fmt.Println(newstr)
fmt.Println(len(str), cap(str), "final")
fmt.Println(len(newstr), cap(newstr), "final")
}
Output:
[dog cat cat mouse mouse zebra zebra]
[dog cat mouse zebra]
[dog cat mouse zebra]
4 7 final
4 7 final
答案2
得分: 2
在main
函数中,有没有办法让str
返回rmDup()
函数中定义的大小和容量呢?
例如,
package main
import "fmt"
// 从字符串数组中删除相邻重复的字符串
func rmDup(str []string) []string {
for i := 1; i < len(str); i++ {
if str[i] == str[i-1] {
copy(str[i:], str[i+1:])
str[len(str)-1] = ""
str = str[:len(str)-1]
}
}
return str
}
func main() {
str := []string{"dog", "cat", "cat", "mouse", "mouse", "zebra", "zebra"}
fmt.Println(len(str), cap(str), str)
str = rmDup(str)
fmt.Println(len(str), cap(str), str)
}
输出结果:
7 7 [dog cat cat mouse mouse zebra zebra]
4 7 [dog cat mouse zebra]
我是说在不让rmDup()
函数返回任何内容的情况下,有没有办法在原地修改str
呢?
例如,
package main
import "fmt"
// 从字符串数组中删除相邻重复的字符串
func rmDup(str *[]string) {
s := *str
for i := 1; i < len(s); i++ {
if s[i] == s[i-1] {
copy(s[i:], s[i+1:])
s[len(s)-1] = ""
s = s[:len(s)-1]
}
}
*str = s
}
func main() {
str := []string{"dog", "cat", "cat", "mouse", "mouse", "zebra", "zebra"}
fmt.Println(len(str), cap(str), str)
rmDup(&str)
fmt.Println(len(str), cap(str), str)
}
输出结果:
7 7 [dog cat cat mouse mouse zebra zebra]
4 7 [dog cat mouse zebra]
英文:
> Is there any way that str
in main
can return the size and capacity
> defined in rmDup()
?
For example,
package main
import "fmt"
//String remove adjacent duplicates from a string array
func rmDup(str []string) []string {
for i := 1; i < len(str); i++ {
if str[i] == str[i-1] {
copy(str[i:], str[i+1:])
str[len(str)-1] = ""
str = str[:len(str)-1]
}
}
return str
}
func main() {
str := []string{"dog", "cat", "cat", "mouse", "mouse", "zebra", "zebra"}
fmt.Println(len(str), cap(str), str)
str = rmDup(str)
fmt.Println(len(str), cap(str), str)
}
Output:
7 7 [dog cat cat mouse mouse zebra zebra]
4 7 [dog cat mouse zebra]
> i meant in place without the function rmDup()
having to return
> anything.
For example,
package main
import "fmt"
//String remove adjacent duplicates from a string array
func rmDup(str *[]string) {
s := *str
for i := 1; i < len(s); i++ {
if s[i] == s[i-1] {
copy(s[i:], s[i+1:])
s[len(s)-1] = ""
s = s[:len(s)-1]
}
}
*str = s
}
func main() {
str := []string{"dog", "cat", "cat", "mouse", "mouse", "zebra", "zebra"}
fmt.Println(len(str), cap(str), str)
rmDup(&str)
fmt.Println(len(str), cap(str), str)
}
Output:
7 7 [dog cat cat mouse mouse zebra zebra]
4 7 [dog cat mouse zebra]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论