Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Maps and Sets in C++

Posted on 2006-11-14
8
Medium Priority
?
505 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

Technology Partners: 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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

610 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