2012-06-25 5 views

답변

6

프로토콜 버퍼 AFAIK에는이 작업을 수행 할 수있는 기본 방법이 없습니다. 물론 docs은 이러한 옵션을 나타내는 것 같지 않습니다.

합리적으로 효율적인 방법은 정상적으로 새 요소를 추가 한 다음 요소를 역순으로 반복하여 이전 요소 앞에있는 새 요소를 목록의 맨 앞에 올 때까지 교체하는 것입니다. 그래서 예. 같은 protobuf 메시지를 :

message Bar { 
    repeated bytes foo = 1; 
} 

당신은 할 수 :

Bar bar; 
bar.add_foo("two"); 
bar.add_foo("three"); 

// Push back new element 
bar.add_foo("one"); 
// Get mutable pointer to repeated field 
google::protobuf::RepeatedPtrField<std::string> *foo_field(bar.mutable_foo()); 
// Reverse iterate, swapping new element in front each time 
for (int i(bar.foo_size() - 1); i > 0; --i) 
    foo_field->SwapElements(i, i - 1); 

std::cout << bar.DebugString() << '\n'; 
관련 문제