C++ - Most efficient way to remove an element from a Message's repeated array

37 views
Skip to first unread message

jaypipes

unread,
Oct 5, 2009, 1:33:07 PM10/5/09
to Protocol Buffers
Hi all,

I'm wondering what is the most efficient method to remove an element
from a Message's vector of repeated elements? I looked at the
RepeatedFieldPtr API but would appreciate some example code on how to
do this in the most efficient way. I gather from the API that the
underlying storage is a vector and so does not support efficient index-
based, random-access deletion from the vector. Is this correct?

Help is much appreciated,

Jay

Kenton Varda

unread,
Oct 5, 2009, 3:56:17 PM10/5/09
to jaypipes, Protocol Buffers
Yes, that is correct.

The most efficient solution is to permute the contents so that the things you want to remove are at the end, then use RemoveLast().

Note that the underlying storage is an array of *pointers*, so re-arranging pointers doesn't require copying whole message objects.

jaypipes

unread,
Oct 5, 2009, 4:14:28 PM10/5/09
to Protocol Buffers
Gotcha. Thanks for the assistance, Kenton!
Reply all
Reply to author
Forward
0 new messages