# [C++] Need Help with a Set within a Map

This topic is 4022 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, maybe you guys can help me out. Using QT I have an std::map with a QString key and a std::set<QLayout *> value, and I need to know how to get at the value contained in the set. In this code, I'm trying to add a QLayout * to a menu, but "layoutItor->second" is a std::set<QLayout *>. I can't seem to dereference it, if that's even what I need to do.
for(map<QString, set<QLayout *> >::iterator layoutItor = layoutMap.begin(); layoutItor != layoutMap.end(); [pp]layoutItor)
{
}



##### Share on other sites
What is the signature of AddLayout?

If it's not
AddLayout( std::set< QLayout * >)
then this won't work.

Which element from the set do you want? You might want to rewrite everything to make it clearer.

map<QString, set<QLayout *> >::iterator layoutItor;for( layoutItor = layoutMap.begin(); layoutItor != layoutMap.end(); ++layoutItor){  std::set< QLayout * > &s = layoutItor->second;  // do something with std::set s}

But which set do you need? You still need to extract a single QLayout * from s.

##### Share on other sites
Is there only a single QLayout* in the set? You did say you wanted to "get at *the* value". If there's only one value in the set, why bother using a container at all?

When you use the -> operator on a map<>::iterator is yields a pointer to an std::pair<>, in your case, an std::pair<const QString, set<QLayout *> >.

So layoutItor->second is giving you the second element of such a pair, which is a set<QLayout *>.

Is this making any sense? :)

##### Share on other sites
Quote:
 Original post by the_eddIs there only a single QLayout* in the set? You did say you wanted to "get at *the* value". If there's only one value in the set, why bother using a container at all?The body of your loop is trying to call menuLayout->addLayout() with a std::set as an argument. There is no such overload.When you use the -> operator on a map<>::iterator is yields a pointer to an std::pair<>, in your case, an std::pair >.So layoutItor->second is giving you the second element of such a pair, which is a set.Is this making any sense? :)

No, the reason I'm iterating over the layoutMap is that I need to get at all the QLayout *s contained in it and add them to menuLayout (all this trouble is because the code needs to be completely dynamic as its building menus from an XML file that will change from time to time)

I guess I need to do what Antheus suggested and first get the set, then iterate over that to get at all the QLayout *s. I believe that will get me what I need.

##### Share on other sites
Quote:
 Original post by JimDanielUsing QT I have an std::map with a QString key and a std::set value, and I need to know how to get at the value contained in the set.

Say what? You have a set of pointers to QLayout objects? That makes no sense.

If you have multiple pointers associated with each label you're better off using a vector rather than a set. Unless you have thousands of QLayouts in your widget, in which case you will need a much faster computer than is currently being manufactured. On the other hand, that's exactly what a multimap is for.

But since QLayouts end up getting owned and deleted by another object, are you not worried about bizarre and unsightly crashes?

--smw

##### Share on other sites
Quote:
 Original post by BregmaSay what? You have a set of pointers to QLayout objects? That makes no sense.If you have multiple pointers associated with each label you're better off using a vector rather than a set. Unless you have thousands of QLayouts in your widget, in which case you will need a much faster computer than is currently being manufactured. On the other hand, that's exactly what a multimap is for.But since QLayouts end up getting owned and deleted by another object, are you not worried about bizarre and unsightly crashes?--smw

To be honest I'm extremely new with C++ and pointers (used to C#), but I guess you can probably tell that from my question. Thanks for the suggestion. Its obvious to me now that I need to rethink my approach...

1. 1
2. 2
3. 3
Rutin
22
4. 4
frob
18
5. 5

• 33
• 13
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
632567
• Total Posts
3007113

×