如何删除重复字段中的任意对象? (protobuf) [英] How to delete arbitrary objects in repeated field? (protobuf)
问题描述
我在proto的重复字段中有一些条目。现在我想删除其中一些。我该如何做到这一点?有一个函数来删除最后一个元素,但我想删除任意元素。我不能只交换它们,因为顺序很重要。
I have some entries in the repeated field in my proto. Now I want delete some of them. How can I accomplish this? There is a function to delete the last element, but I want to delete arbitrary elements. I cant just swap them because the order is important.
我可以交换下一个直到结束,但是没有更好的解决方案?
I could swap with next until end, but isn't there a nicer solution?
推荐答案
根据 API文档,没有办法从重复字段中任意删除元素,只是删除最后一个元素的方法。
According to the API docs, there isn't a way to arbitrarily remove an element from within a repeated field, just a way to remove the last one.
...
我们不提供删除最后
以外的任何元素的方法,因为它会导致无效的使用,例如作为O(n ^ 2)过滤循环
,应该已经是O(n)。如果你想删除一个元素其他
比最后一个,最好的方法是重新排列元素所以
,你想要删除的是在结束,然后调用RemoveLast()
...
...
We don't provide a way to remove any element other than the last because it invites inefficient use, such as O(n^2) filtering loops that should have been O(n). If you want to remove an element other than the last, the best way to do it is to re-arrange the elements so that the one you want removed is at the end, then call RemoveLast()
...
这篇关于如何删除重复字段中的任意对象? (protobuf)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!