Sign in to follow this  

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

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

If you intended to correct an error in the post then please contact us.

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)
{
    menuLayout->addLayout(layoutItor->second);
}


Please help me if you can...

Share this post


Link to post
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 this post


Link to post
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?

The body of your loop is trying to call menuLayout->addLayout() with a std::set<QLayout *> 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<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 this post


Link to post
Share on other sites
Quote:
Original post by the_edd
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?

The body of your loop is trying to call menuLayout->addLayout() with a std::set<QLayout *> 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<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? :)


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.

Thanks for the advice!

Share this post


Link to post
Share on other sites
Quote:
Original post by JimDaniel
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.


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 this post


Link to post
Share on other sites
Quote:
Original post by Bregma
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


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...

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this