1

For a particular key I want to insert and print elements of the set corresponding to that key. For, e.g. if I have A - Orange, apple B - Red, blue

How do I print this? So far I have written this:`

std::map<string,std::set<string> > mp;
std::map<string,std::set<string> >::const_iterator row;
std::set<string>:: const_iterator col;

mp["A"].insert("pawan");
mp["A"].insert("patil");

for (row = mp.begin(); row!= mp.end(); row++)
    for (col = row->begin(); col!=row.end(); col++)
return 0;`

I don't know how to begin. Please help!`

3 Answers 3

3
for(auto const& pair : mp) {
    cout << pair.first << ": ";
    for(auto const& elem : pair.second) {
        cout << elem << ", ";
    }
    cout << "\n";
}

live example

Or, if you want to use std algorithms more:

std::for_each(mp.cbegin(), mp.cend(), [](auto const& pair){
    cout << pair.first << ": ";
    std::copy(pair.second.cbegin(), pair.second.cend(), std::ostream_iterator<std::string>(std::cout, ", "));
    cout << "\n";
});

live example

Sign up to request clarification or add additional context in comments.

4 Comments

How to declare pair and elem? Sorry, I'm new to C++.
They're declared right here as auto const& pair and auto const& elem. This allows the compiler to deduce their actual types and you don't have to specify the long type name yourself. Added live example.
I am getting this error: error: ISO C++ forbids declaration of ‘pair’ with no type [-fpermissive] for(auto const& pair : mp)
What's your compiler? Maybe you're using something ancient. For anything non-ancient, try adding -std=c++11 to the command line (assuming clang/gcc).
0

The question wants to insert an element, and then print the set for that key only.

The first step is to find the set:

auto &s=mp["A"];

Now, insert values into this set:

s.insert("pawan");
s.insert("patil");

And now, iterate over the set, printing the values in the set:

for (const auto &v:s)
    std::cout << v << std::endl;

Comments

0
for(auto it=mp.begin();it!=mp.end();++it)  //Loop to iterate over map elements
 {
    cout<<it->first<<"=";    
    for(auto it1=it->second.begin(); it1 !=it->second.end(); it1++)
        cout<<*it1<<" ";
    cout<<"\n";   
}

The outer for loop iterates over all the elements of map and Inner for loop prints the value corresponding to set associated with the key in the map.

2 Comments

While this code may provide a solution to the question, it's better to add context as to why/how it works. This can help future users learn, and apply that knowledge to their own code. You are also likely to have positive feedback from users in the form of upvotes, when the code is explained.
Also please indent your code properly, this is just atrocious, that for statement should be on the same level as the couts

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.