c++ - Delete a random value from repeated string ( list ) in Google Protocol buffer -
i want delete item repeated field
for have message defination:
message foo { repeated string temp1 ; repeated string temp2 ; } i want remove item temp1 @ random index;
as per knowledge can delete swapping last element , using removelast; dont know how use that. code snapshot in c++ ?
here 1 reason why there no remove() in protocol buffer.
i didn't want provide
remove(int)becauseo(n). version 1 of protocol buffers had such function, , saw people writing loops like:(int = 0; < field.size(); i++) { if (shouldfilter(field[i])) { field.remove(i); --i; } }this loop
o(n^2), bad, it's hard tello(n^2). idea behind providingremovelast()force either clever (like swapping element last element first, documentation suggests) or write own loop makes time complexity of code obvious.
two options here:
copy item end of list space formerly occupied item want delete, call
removelast().by using
iterator erase(const_iterator position), should startbegin(), check whether value of iterator should removed.
Comments
Post a Comment