迭代器列表,用于从各种列表中收集条目 [英] List of iterators to collect entries from various lists

查看:220
本文介绍了迭代器列表,用于从各种列表中收集条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TaskGroup主要包含Task的列表.我想从各个组gagbgc,...中收集某些任务ta1ta2tb3,...,并在显示窗口/类中使用此列表.集合任务的选择与这个问题无关.

A class TaskGroup mainly contains a list of Tasks. I want to collect certain tasks ta1, ta2, tb3, ... from various groups ga, gb, gc, ... and use this list in a display window/class. The choice of tasks for the collection is of no concern for this question.

(由于问题不是概念性的,而是技术性的,因此示例大多是伪代码.由于我在这种特殊情况下使用Qt,因此我将使用Qt类,但问题不应仅限于这种情况)

(As the question is rather conceptual than technical, the examples are mostly pseudo code. Since I'm using Qt in this particular case, I'll be using Qt classes, but the question shouldn't be limited to this case.)

class Task;

class TaskGroup {
    QString name;
    QList<Task> tasklist;
    ...
}

以上文本中的对象ta1等应理解为组ga中的第一项任务"等.

The objects from the text above, ta1 etc, should be understood as "first task from group ga" etc.

现在,这将不是问题:

QList<Task> collection;
collection.append(ga.getTask(1));
...
collection.append(gb.getTask(3));
...

doSomethingWithTheCollection(collection);

用例类似于创建以列表形式组织的任务->选择某些任务->将它们添加到待办事项列表(集合)->显示集合->履行任务->删除已完成的任务集合中的原始列表,一次一次->重复". 前述实现的问题在于,我无法删除原始组中的条目(甚至不能通过引用或指针传递),因为仅对象是集合的一部分.

The use case is something like "create tasks organized in lists --> choose certain tasks --> add them to a TO DO list (collection) --> display collection --> fulfill tasks --> delete accomplished task from collection AND its original list one at a time --> repeat". The problem with the aforementioned implementation is that I cannot delete the entry in the original group (not even by passing by reference or pointer), because just the object is part of the collection.

因此,我需要将集合设为迭代器列表!

I therefore would need to make the collection a list of iterators!

QList<QList<Task>::iterator> collection;
collection.append(ga.getIteratorToTask(1));
...

因为我可以通过取消引用使用相应的任务,完成后可以通过collection[x].erase();删除它们,它们将从原始列表中消失.

Because I can then use the respective tasks via dereferencing and upon completion I can erase it by collection[x].erase(); and they disappear from the original list.

我到目前为止想的还对吗?还是这是一个复杂的概念?

Am I thinking right so far? Or is this even a sophisticated concept?

如果集合中每个列表中有多个任务,则我必须使用QLinkedList,因为在操纵了常规QList之后,迭代器将变得无效...因此,我应该使用以下方法实现TaskGroup类吗? QLinkedList代替QList?

If there is more than one quest per list in the collection I have to use QLinkedList, because after manipulation of a general QList, iterators become invalid... Should I therefore implement the TaskGroup class with a QLinkedList instead of a QList?

在将该概念付诸实践的同时,我很失望地发现erase(iterator)QList类的一部分,而不是迭代器本身.看来,因此我无法像希望的那样仅通过迭代器删除原始列表中的条目...我需要该列表.有什么解决办法吗? 也许使用类似Java的QMutableListIterator及其remove()方法而不是QList<Task>::iterator? QMutableListIterator的缺点是不支持迭代器算法". (我不想传递整个列表...我可以传递指向各个QList<Task>::erase()函数的函数指针吗?我知道这听起来很疯狂,但是在这一点上,我想知道我的方法是否可行. )

While putting that concept into practice, I discovered in disappointment that erase(iterator) is part of the QList class and not of the iterator itself. It seems, I thus cannot delete the entry in the original list, as I wished, via just the iterator... I need the list for that. Is there any solution to that? Maybe using the Java-like QMutableListIterator and its remove() method instead of QList<Task>::iterator? QMutableListIterator has the disadvantage of not supporting "iterator arithemtics". (I don't want to pass the entire list...could I pass a function pointer to the respective QList<Task>::erase() function? I know this sounds crazy but at this point, I want to know if my approach is somehow doable...)

推荐答案

我认为您的解决方案听起来很合理,并且您正确地需要使用QLinkedList以避免迭代器无效.

I think your solution sounds reasonable, and you are correct that you would need to use a QLinkedList to avoid iterator invalidation.

另一种设计是给Task一个拥有它们的TaskGroup的反向指针,并向TaskGroup添加一个方法以删除Task.完成后,Task可以请求将其从TaskGroup中删除.

An alternative design would be to give Tasks a back-pointer to the TaskGroup that owns them, and add a method to TaskGroup to remove a Task. The Task can then request that it is removed from the TaskGroup when it has been completed.

类似于以下内容(我没有尝试过):

Something like the following (which I haven't tried):

void Task::Complete()
{
    parent->RemoveTask(*this);
}

void TaskGroup::RemoveTask(Task& task)
{
    tasklist.removeAll(task);
}

这篇关于迭代器列表,用于从各种列表中收集条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆