在使用 Repeated 类型 链表结构 使用add_foo() 以后,要注意clear_foo() 释放内存空间,最好的办法是在该链表类的析构方法中执行 this->clear_foo();
Repeated Embedded Message Fields
Given the message type:
message Bar {}
For this field definitions:
repeated Bar foo = 1;
The compiler will generate the following accessor methods:
-
int foo_size() const: Returns the number of elements currently in the field. -
const Bar& foo(int index) const: Returns the element at the given zero-based index. -
Bar* mutable_foo(int index): Returns a mutable pointer to theBarobject that stores the value of the element at the given zero-based index. The pointer is invalidated by a call toClear()orclear_foo(), or by manipulating the underlyingRepeatedPtrFieldin a way that would remove this element. -
Bar* add_foo(): Adds a new element and returns a pointer to it. The returnedBarwill have none of its fields set (i.e. it will be identical to a newly-allocatedBar). The pointer is invalidated by a call toClear()orclear_foo(), or by manipulating the underlyingRepeatedPtrFieldin a way that would remove this element. -
void clear_foo(): Removes all elements from the field. After calling this,foo_size()will return zero. -
const RepeatedPtrField<Bar>& foo() const: Returns the underlyingRepeatedPtrFieldthat stores the field's elements. This container class provides STL-like iterators and other methods. -
RepeatedPtrField<Bar>*mutable_foo(): Returns a mutable pointer to the underlyingRepeatedPtrFieldthat stores the field's elements. This container class provides STL-like iterators and other methods.
本文介绍了如何管理和操作Protobuf消息格式中Repeated类型的Bar字段。包括获取元素数量、访问特定索引处的元素、获取可修改的指针、添加新元素及释放内存等方法。
1610

被折叠的 条评论
为什么被折叠?



