Solved

Maps and Sets in C++

Posted on 2006-11-14
8
499 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 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

734 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