我的第一个尝试是:删除元素
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