简体   繁体   English

关于结构中动态数组的迭代器

[英]Iterator on dynamic array in a struct

如何在动态数组(在结构中)上创建迭代器?

Your dynamic array, as represented by type struct _Lista , does not contain "elements of any type". 您的动态数组(由struct _Lista类型表示) 包含“任何类型的元素”。 At least not directly. 至少不是直接的。 It contains only and exactly elements of type void * . 它仅包含和确切的void *类型的元素。 Pretty much everything you've written will work if you change type iterator accordingly, to a double pointer: 如果你相应地将类型iterator更改为指针,那么你编写的所有内容都会有效:

typedef void **iterator;

Do note, however, that this iterator implementation has a severe flaw: it does not carry any information about the bounds of the list. 但请注意,此迭代器实现存在严重缺陷:它不包含有关列表边界的任何信息。 That may be OK if you ensure that there is a sentinel value after the last valid element (and maybe before the first, too), but otherwise you'll need a more complex data structure than just one pointer. 如果你确保在最后一个有效元素之后有一个sentinel值(也可能在第一个有效元素之前),那可能没问题,但是否则你需要一个比一个指针更复杂的数据结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM