修改切片副本中的值然後附加到原始切片
在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 通過編譯器優化和標準庫,提供接近硬件的高性能,適合需要極致優化的應用。

goimpactsdevelopmentpositationality throughspeed,效率和模擬性。 1)速度:gocompilesquicklyandrunseff,IdealforlargeProjects.2)效率:效率:ITScomprehenSevestAndardArdardArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdEcceSteral Depentencies,增強的Depleflovelmentimency.3)簡單性。

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

Golang適合快速開發和並發場景,C 適用於需要極致性能和低級控制的場景。 1)Golang通過垃圾回收和並發機制提升性能,適合高並發Web服務開發。 2)C 通過手動內存管理和編譯器優化達到極致性能,適用於嵌入式系統開發。

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

C 更適合需要直接控制硬件資源和高性能優化的場景,而Golang更適合需要快速開發和高並發處理的場景。 1.C 的優勢在於其接近硬件的特性和高度的優化能力,適合遊戲開發等高性能需求。 2.Golang的優勢在於其簡潔的語法和天然的並發支持,適合高並發服務開發。

Golang和C 在性能上的差異主要體現在內存管理、編譯優化和運行時效率等方面。 1)Golang的垃圾回收機制方便但可能影響性能,2)C 的手動內存管理和編譯器優化在遞歸計算中表現更為高效。
