?
Solved

Maps and Sets in C++

Posted on 2006-11-14
8
Medium Priority
?
502 Views
Last Modified: 2011-10-03
Does anyone know how to have a map that returns a set of strings in C++?
0
Comment
Question by:FrankeButler
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 672 total points
ID: 17938617
You mean:

    map<string, set<string>> myMap;

?
0
 
LVL 5

Assisted Solution

by:NickGeorghiou
NickGeorghiou earned 664 total points
ID: 17938733
Hi FrankeButler,

I like using typedefs personally, but effectively this is the same thing as carl_tawn has specified above,

--------------------

typedef std::set<std::string> tMyStringSet;
typedef tMyStringSet::iterator tSetIt;
typedef std::map<std::string, tMyStringSet> tMyStringSetMap;
typedef tMyStringSetMap::iterator tMapIt;

tMyStringSetMap myStringSetMap;

--------------------



Example of insertion into map:

--------------------

tMyStringSet aSet;
aSet.insert("test1");
aSet.insert("test2");

myStringSetMap["aSet"] = aSet;

--------------------


Example of retrieval from map:

--------------------

std::string findStr = "aSet";
tMapIt it = myStringSetMap.find(findStr);

if (it!= myStringSetMap.end())
{
    for (tSetIt it2 = it->second.begin(); it2 != it->second.end(); it2++)
    {
        std::string test = *it2;
    }
}

--------------------


Cheers,
Nick
0
 

Author Comment

by:FrankeButler
ID: 17939316
NickGeorghiou,

Thanks a lot. I have a question though. How would I go about printing all the entries of each set in the entire map? And also, how would I go about printing all the entries of a set for just one key value in the map?

Thanks a million.
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 664 total points
ID: 17939460
>>>> how would I go about printing

Maybe like that:

void printset(std::set<std::string>& s)
{
    std::set<std::string>::iterator i;
    for (i = s.begin(); i != s.end(); ++i)
         cout << *i << endl;
}

void printmap(std::map<std::string, std::set<std::string> >& m)
{
    std::map<std::string, std::set<std::string> >::iterator i;
    for (i = m.begin(); i != m.end(); ++i)
    {
         cout << i->first << endl;
         printset(i->second);
    }
}

Regards, Alex
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 19203440
I would say all answers were equally valid. Carl gave a (short) answer to the initial question. Nick gave samples for inserting and retrieving. And I answered the additional question for printing by full sample code. So a split seems appropriate.

Regards, Alex
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question