Iterator as a member: Tree/Graph-like structure
-
- Christian's approach of defining a
<operator for theTaskstruct itself is required if and only if you wish to have aTask *as the key for theQMap. Which is what he says you had stated initially. - But I don't see why you would want or need that (my
Task *is the value, not the key). I just use anintas the key and passtask->idfor that at map insert time.
Up to you.
@JonB said in Iterator as a member: Tree/Graph-like structure:
I just use an int as the key and pass task->id for that at map insert time
Yeah that makes sense... but my concern is/was that I have some redundancy. MyTask::id = key AND also already stored in
MyTask(and accessible via something likevalue().id)...Will try it out.
- Christian's approach of defining a
-
@JonB said in Iterator as a member: Tree/Graph-like structure:
I just use an int as the key and pass task->id for that at map insert time
Yeah that makes sense... but my concern is/was that I have some redundancy. MyTask::id = key AND also already stored in
MyTask(and accessible via something likevalue().id)...Will try it out.
@Pl45m4
You are saving/losing anintfor the key. But even another way to use aQMapyou have to provide some key to go with a value. If you useTask *as the key that costs a pointer (even if you also storeTask *as the value too) which is actually bigger than anint. Plus yourQMapactually goes wrong if you go change what the key pointer points to, or theidinside that, if you change the id in the Task yourQMapwon't rearrange itself!Many times we do key-value pairs like this. I could be wrong, but when, say, you have a database table with a primary (or unique) key/index I don't think that stores a "pointer to" its value somewhere in the row for its data, I think it copies the value to the index and then keeps that in sync if the row changes.
But if you are happier with the key as a
Task *and override the<operator that is fine too. -
Sorry but I don't understand what you mean - simply replace QMap<MyTask *, whatever> with QMap<Key, whatever> and provide a operator<() for the key (I was wrong above - you don't have to provide a qHash() but a operator <() for a QMap)
struct Key { MyTask *task; bool operator <(const Key &o) const { return task->id < o.task->id; } }; QMap<Key, something> myMap;@Christian-Ehrlicher said in Iterator as a member: Tree/Graph-like structure:
simply replace QMap<MyTask *, whatever> with QMap<Key, whatever> and provide a operator<() for the key (I was wrong above - you don't have to provide a qHash() but a operator <() for a QMap)
struct Key { MyTask *task; bool operator <(const Key &o) const { return task->id < o.task->id; } }; QMap<Key, something> myMap;One more thing @Christian-Ehrlicher :
Is there a reason why you picked an extra struct for theKeyinstead of using theMyTask::operator <directly? -
@Christian-Ehrlicher said in Iterator as a member: Tree/Graph-like structure:
simply replace QMap<MyTask *, whatever> with QMap<Key, whatever> and provide a operator<() for the key (I was wrong above - you don't have to provide a qHash() but a operator <() for a QMap)
struct Key { MyTask *task; bool operator <(const Key &o) const { return task->id < o.task->id; } }; QMap<Key, something> myMap;One more thing @Christian-Ehrlicher :
Is there a reason why you picked an extra struct for theKeyinstead of using theMyTask::operator <directly?@Pl45m4 said in Iterator as a member: Tree/Graph-like structure:
instead of using the MyTask::operator < directly?
Because this operator would not be used by a QMap<MyTask*, ...> as you store a pointer, not a value.
-
@Pl45m4 said in Iterator as a member: Tree/Graph-like structure:
instead of using the MyTask::operator < directly?
Because this operator would not be used by a QMap<MyTask*, ...> as you store a pointer, not a value.
Ah I see, thanks.
-
How many elements would there be in this container?
-
-
An "inefficient" simple for loop may very well be the most efficient after all. When you have 50 elements it won't matter much anyway, pick the container with the friendliest API depending on how you plan to access it.
-
An "inefficient" simple for loop may very well be the most efficient after all. When you have 50 elements it won't matter much anyway, pick the container with the friendliest API depending on how you plan to access it.
@GrecKo said in Iterator as a member: Tree/Graph-like structure:
pick the container with the friendliest API depending on how you plan to access it.
Still what @Christian-Ehrlicher and @JonB suggested doesn't sound too bad :)
Will definitely try it.Similar question: Why is
QButtonGroupusing aQHash-map for its member buttons? :))
I doubt that there ever will be thousands of button widgets in one group :)