0

currently I am trying to solve two problems when working with iterators.

1 When using something like

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

is it possible to get the actual position/index of iterator "iter" (which pointer is the iterator refering to at the moment)? I know that for the vector class I could use something like described in the following link

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

but unfortunately the class "PtrDictionary" which I have to use does not offer a method "begin()". Furthermore "PtrDictionary" is basically of type "intrusive doubly linked list".

2

Old Question:

If I define a dictionary like

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

Can I create an iterator for the dictionary, set this iterator to a specific position of the dictionary (the position I would get from the first iterator if possible -> see code snippet 1) and get the content of the dictionary at that position?

I have read the following thread

how to get iterator to a particular position of a vector

but again class "dictionary" does not have any method "begin()".

Edit/New: I have used now a list (which is of "type" vector) like

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

The elements of the vector should be accessable like an array as far as I have read :).

However, the type of the object phases_ which is of type PtrDictionary<phaseModel> (parents: DictionaryBase< IDLListType, T > and parent of this: IDLListType) which is mentioned in question 1 I can t change.

Any hints, ideas or code solutions are welcome :)

greetings streight

Community
  • 1
  • 1
Streight
  • 811
  • 4
  • 15
  • 28
  • So doesn't dictionary use some type of container underneath the hood? If not, how was your iterator initialized? It had to "point" somewhere first. Also, if your dictionary *is* a container, then you need to make it STL compliant by supplying an iterator for it that has begin(), end(), etc. member functions. – PaulMcKenzie Feb 18 '14 at 20:55
  • The first code sample is very confusing, and barely looks like C++. What are `forAllIter`, `PtrDictionary`, `phaseModel`, `phases_`, and `iter`? – aschepler Feb 18 '14 at 21:09
  • @aschepler : `forAllIter` is a macro, generally a kind of for loop where the iterator `iter` iterates through all elements of `phases_` which is of type `PtrDictionary` (a pointer dictionary). – Streight Feb 19 '14 at 08:53

1 Answers1

1

Sometimes an iterator corresponds to an index that can easily be found, for example a vector iterator. Here you use distance(vector.begin(), iter);

Sometimes an iterator corresponds to an index that cannot be easily found, for example a list iterator. Here you have to start at begin() and work through, counting as you go. distance() will do this if needed but its obviously a lot slower.

Sometimes the idea of an 'index' does not really apply - I think an unsorted map might be a good example of this. distance will still return a value but it doesn't mean much I think. It might even be implementation dependent - I would have to look that up.

Tim Bergel
  • 499
  • 3
  • 9