gpt4 book ai didi

c++ - 获取迭代器在基类型 "intrusive doubly linked list"的指针字典中的位置

转载 作者:行者123 更新时间:2023-11-28 07:05:13 25 4
gpt4 key购买 nike

目前我正在尝试解决使用迭代器时遇到的两个问题。

1当使用类似的东西时

forAllIter(PtrDictionary<phaseModel>, phases_, iter)  
{
phaseModel& phase = iter();
.
.
.
}

是否可以获取迭代器“iter”的实际位置/索引(此时迭代器指的是哪个指针)?我知道对于 vector 类,我可以使用以下链接中描述的内容

What is the most effective way to get the index of an iterator of an std::vector?

但不幸的是,我必须使用的“PtrDictionary”类没有提供“begin()”方法。此外,“PtrDictionary”基本上属于“侵入式双向链表”类型。

2

Old Question:

If I define a dictionary like

const dictionary& subDict_Droplets;
subDict_Droplets = properties.subDict("currentValue");

我可以为字典创建一个迭代器吗,将这个迭代器设置为 字典的特定位置(我将从 如果可能,第一个迭代器 -> 查看代码片段 1) 并获取内容 那个位置的字典?

我已阅读以下主题

how to get iterator to a particular position of a vector

但是类“dictionary”同样没有任何方法“begin()”。

编辑/新建:我现在使用了一个列表(属于“类型” vector )

const List<dimensionedScalar> List_Droplets;
List_Droplets = properties.subDict("currentValue");

据我所知, vector 的元素应该像数组一样可访问:)。

然而,对象的类型phases_这是类型 PtrDictionary<phaseModel> ( parent :DictionaryBase< IDLListType, T > 和 parent :IDLListType )这是在问题中提到的 1我无法改变。

欢迎任何提示、想法或代码解决方案:)

直接问候

最佳答案

有时一个迭代器对应一个很容易找到的索引,例如 vector 迭代器。这里使用 distance(vector.begin(), iter);

有时一个迭代器对应一个不容易找到的索引,例如一个列表迭代器。在这里你必须从 begin() 开始并完成,边走边数。 distance() 会在需要时执行此操作,但显然要慢很多。

有时“索引”的概念并不适用——我认为未排序的 map 可能就是一个很好的例子。距离仍然会返回一个值,但我认为这并不重要。它甚至可能依赖于实现 - 我必须查一下。

关于c++ - 获取迭代器在基类型 "intrusive doubly linked list"的指针字典中的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21864662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com