Solved

Maps and Sets in C++

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

823 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