C语言 C语言迭代器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4961860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:45:19  来源:igfitidea点击:

Iterator in C language

c

提问by Avinash

Has anyone tried providing support for Iterator in C. I am not looking for exact C++ STL::Iterator but minimal support for some idea to start would be good point for me .

有没有人尝试在 C 中提供对 Iterator 的支持。我不是在寻找确切的 C++ STL::Iterator,但对一些想法的最小支持对我来说是个好点子。

I am developing container library same like stl but with minimal support, So I need this kind of functionality in those container.

我正在开发与 stl 相同的容器库,但支持最少,所以我需要在这些容器中使用这种功能。

I am looking forward of defining certain sets of algorithms interfaces ( similar to STL ). For example sort , which will take begin and end iterator and should work with any container.

我期待定义某些算法接口集(类似于 STL)。例如 sort ,它将采用开始和结束迭代器,并且应该适用于任何容器。

采纳答案by jdehaan

If you are allowed to use GPL code in your project have a look at GLib instead of re-inventing the wheel. GLib allows also to develop in a quite portable way at source code level. Note that if you use this you have to release the code also under GPL.

如果您被允许在您的项目中使用 GPL 代码,请查看 GLib 而不是重新发明轮子。GLib 还允许在源代码级别以一种非常可移植的方式进行开发。请注意,如果您使用它,您还必须在 GPL 下发布代码。

Have a look at g_list_first()and g_list_next()which implement the functionality of an iterator on the list. There is even a g_list_foreach()`

看看g_list_first(),并g_list_next()实现了这个名单上的迭代器的功能。甚至还有一个 g_list_foreach()`

http://library.gnome.org/devel/glib/stable/glib-Doubly-Linked-Lists.html

http://library.gnome.org/devel/glib/stable/glib-Doubly-Linked-Lists.html

回答by AShelly

Pointers can serve this function. container.begin()is easy, and container.end()doesn't take too much work.

指针可以提供此功能。 container.begin()很容易,container.end()不需要太多工作。

Consider

考虑

Value array[N];
typedef Value* iterator;
iterator array_begin(Value a[]){ return &a[0];}
iterator array_end(Value a[], int n){ return &a[n];}
iterator array_next(iterator i) { return ++i;}

iterator it = array_begin(a);
iterator end = array_end(a,N);
for (;it < end; it=array_next(it))
{
    Value v = *it;
}

For other containers like lists, you can use NULL as end. Same for trees, but the nextfunction needs to maintain state. (or the iterator is a pointer to a struct with state updated by calls to next(it)).

对于列表等其他容器,您可以使用 NULL 作为结束。树也一样,但next函数需要维护状态。(或者迭代器是一个指向结构的指针,该结构的状态通过调用 更新next(it))。

回答by Alex Reynolds

Take a look at linked lists. A node includes a "next" pointer that one can use to iterate through the list, in a manner analogous to C++ iterators:

看看链表。一个节点包含一个“下一个”指针,可以用来迭代列表,类似于 C++ 迭代器:

typedef struct Node {
    ...                                                                                                                                                           
    struct Node *next;                                                                                                                                                          
} Node;  

...

Node *iter, *firstNode, *nodeList; 

/* set firstNode and populate nodeList */

for (iter = firstNode; iter != NULL; iter = iter->next) {
    /* iterate through list */
}

It's not a C++ iterator, but hopefully this gives an idea of one way to approach this in C.

它不是 C++ 迭代器,但希望这提供了一种在 C 中解决此问题的方法的想法。

回答by chrisaycock

You'd need a standardized way of incrementingthe iterator. In C++, that's just the overloaded operator++(). Your container needs an associated function that returns a pointer to the next element. This incrementing function would need to be passed as a pointer to any generalized routine that can accept an iterator in your library.

您需要一种标准化的递增迭代器的方法。在 C++ 中,这只是重载的operator++(). 您的容器需要一个关联的函数来返回指向下一个元素的指针。这个递增函数需要作为指针传递给任何可以接受库中迭代器的通用例程。

For example, If I want to write a function that returns the maxelement from the container, I need not only the comparison function (the equivalent of operator<()), I need an iterator-incrementing function (the equivalent of operator++()).

例如,如果我想编写一个从容器中返回最大元素的函数,我不仅需要比较函数(相当于operator<()),还需要一个迭代器递增函数(相当于operator++())。

So ensuring that I can accept a pointer to your incrementing function is the key requirement.

因此,确保我可以接受指向您的递增函数的指针是关键要求。

回答by Spidey

This is what I came up with:

这就是我想出的:

typedef struct PWDict PWDict;
typedef struct PWDictIterator PWDictIterator;

typedef struct PWDictImplementation
{
    PWDict *(*create)(const struct PWDictImplementation *impl, size_t elements);
    void (*destroy)(PWDict *dict);

    unsigned int (*size)(const PWDict *dict);
    unsigned int (*sizeInBytes)(const PWDict *dict);

    int (*get)(const PWDict *dict, const char *key, char *output, size_t size);
    int (*set)(PWDict *dict, const char *key, const char *value);

    PWDictIterator *(*iteratorCreate)(const PWDict *dict);
    void (*iteratorBegin)(PWDictIterator *it);
    void (*iteratorEnd)(PWDictIterator *it);
    void (*iteratorDestroy)(PWDictIterator *it);

    const char *(*iteratorGetKey)(const PWDictIterator *it);
    const char *(*iteratorGetValue)(const PWDictIterator *it);
    int (*iteratorSetValue)(PWDictIterator *it, const char *value);
    void (*iteratorNext)(PWDictIterator *it);
}
PWDictImplementation;

struct PWDict
{
    PWDictImplementation *impl;
};

struct PWDictIterator
{
    PWDict *dict; /* get iterator implementation from the dict implementation */
};

PW is our project prefix. We just needed a dictionary (string-string map) like container.

PW 是我们的项目前缀。我们只需要一个像容器这样的字典(字符串-字符串映射)。

回答by Avinash

I found one open source project which is STL implementation in C language.

我发现了一个开源项目,它是用 C 语言实现的 STL。

http://sourceforge.net/projects/tstl2cl/

http://sourceforge.net/projects/tstl2cl/