Solved

Maps and Sets in C++

Posted on 2006-11-14
8
492 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
8 Comments
 
LVL 52

Accepted Solution

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

    map<string, set<string>> myMap;

?
0
 
LVL 5

Assisted Solution

by:NickGeorghiou
NickGeorghiou earned 166 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 166 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  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 …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now