修改切片副本中的值然后附加到原始切片
在PHP中,切片(Slice)是一种常用的数据结构,它允许我们从一个数组或切片中选取一部分元素。然而,有时候我们需要对切片进行修改,并将修改后的值附加到原始切片中。这种操作在PHP中是可以实现的,通过修改切片副本中的值,然后使用array_splice函数将修改后的副本附加到原始切片中即可。在本文中,我们将详细介绍如何使用这种方法实现切片的修改和附加操作。
问题内容
编写一个函数来复制切片并修改切片副本中项目的值,然后将副本附加到原始副本。看起来代码不仅修改了切片的副本,还修改了原始切片。
去游乐场
import "fmt" type item struct { name string number int } func main() { names := []string{"a", "b"} numbers := []int{1, 2} items := []*item{} for _, name := range names { item := &item{name: name} items = append(items, item) } for n, i := range numbers { if n > 0 { fmt.println(n, "make a copy of the items") itemcopies := make([]*item, len(items)) copy(itemcopies, items) fmt.println(n, "set the numbers on the copies") for _, item := range itemcopies { item.number = i } fmt.println(n, "append the copies to the original") items = append(items, itemcopies...) } else { fmt.println(n, "first pass set all the items to the first number") for _, item := range items { item.number = i } } } for n, i := range items { fmt.printf("%d %+v\n", n, *i) } }
我实际看到的
0 {name:a number:2} 1 {name:b number:2} 2 {name:a number:2} 3 {name:b number:2}
我期望看到什么
0 {name:a number:1} 1 {name:b number:1} 2 {name:a number:2} 3 {name:b number:2}
解决方法
@jimb 是对的,你复制指针,
我建议定义一个属于struct item的copy函数来新建一个副本
package main import "fmt" type Item struct { name string number int } func (it *Item) copy() *Item { return &Item{ name: it.name, number: it.number, } } func main() { names := []string{"a", "b"} numbers := []int{1, 2} items := []*Item{} for _, name := range names { item := &Item{name: name} items = append(items, item) } for n, i := range numbers { if n > 0 { fmt.Println(n, "make a copy of the items") itemCopies := make([]*Item, len(items)) for j := 0; j < len(items); j++ { itemCopies[j] = items[j].copy() } fmt.Println(n, "set the numbers on the copies") for _, item := range itemCopies { item.number = i } fmt.Println(n, "append the copies to the original") items = append(items, itemCopies...) } else { fmt.Println(n, "first pass set all the items to the first number") for _, item := range items { item.number = i } } } for n, i := range items { fmt.Printf("%d %+v\n", n, *i) } }
以上是修改切片副本中的值然后附加到原始切片的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Golang在性能和可扩展性方面优于Python。1)Golang的编译型特性和高效并发模型使其在高并发场景下表现出色。2)Python作为解释型语言,执行速度较慢,但通过工具如Cython可优化性能。

Golang在并发性上优于C ,而C 在原始速度上优于Golang。1)Golang通过goroutine和channel实现高效并发,适合处理大量并发任务。2)C 通过编译器优化和标准库,提供接近硬件的高性能,适合需要极致优化的应用。

goisidealforbeginnersandsubableforforcloudnetworkservicesduetoitssimplicity,效率和concurrencyFeatures.1)installgromtheofficialwebsitealwebsiteandverifywith'.2)

Golang适合快速开发和并发场景,C 适用于需要极致性能和低级控制的场景。1)Golang通过垃圾回收和并发机制提升性能,适合高并发Web服务开发。2)C 通过手动内存管理和编译器优化达到极致性能,适用于嵌入式系统开发。

GoimpactsdevelopmentPositationalityThroughSpeed,效率和模拟性。1)速度:gocompilesquicklyandrunseff,ifealforlargeprojects.2)效率:效率:ITScomprehenSevestAndArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdEcceSteral Depentencies,增强开发的简单性:3)SimpleflovelmentIcties:3)简单性。

Golang和Python各有优势:Golang适合高性能和并发编程,Python适用于数据科学和Web开发。 Golang以其并发模型和高效性能着称,Python则以简洁语法和丰富库生态系统着称。

C 更适合需要直接控制硬件资源和高性能优化的场景,而Golang更适合需要快速开发和高并发处理的场景。1.C 的优势在于其接近硬件的特性和高度的优化能力,适合游戏开发等高性能需求。2.Golang的优势在于其简洁的语法和天然的并发支持,适合高并发服务开发。

Golang和C 在性能上的差异主要体现在内存管理、编译优化和运行时效率等方面。1)Golang的垃圾回收机制方便但可能影响性能,2)C 的手动内存管理和编译器优化在递归计算中表现更为高效。
