我的第一個嘗試是:刪除元素
I = Vector{String}(["first", "second", "third", "fourth"])
for i in I
if i == "fourth"
splice!(I, 4)
end
print("$i\n")
end
其在界失誤結束:
BoundsError(String["first", "second", "third"], (5,))
後來我想通它有點了「手寫」的方式:
I = Vector{String}(["first", "second", "third", "fourth"])
state = start(I)
while ! done(I, state)
(i, state) = next(I, state)
if i == "fourth"
splice!(I, state - 1)
print("Delete element i=$(state - 1)\n")
state = state - 1
continue
end
print("Got: i=$i state=$state\n")
end
輸出:
Got: i=first state=2
Got: i=second state=3
Got: i=third state=4
Delete element i=4
但是,是的,這既不容易閱讀,也不容易寫。是否有任何「朱利安」的方式來刪除矢量元素,而迭代呢?或者是否有推薦的數據結構通過某種函數調用明確支持它?
我不知道你想要什麼,但'啪!(I) '接近你在找什麼? [是的,我不知道這是否與菠菜有關。] – rickhg12hs
@ rickhg12hs哈哈,不幸的是不是我所需要的。我需要能夠從矢量中刪除任何元素,而不管它在迭代時的位置。 – lama12345