2

I'd like to use std::set in std::map

I haven't much exp with std:: containers, so i am not sure if i am using it right. I am trying to process set of values and in each set is another set of values.

map<string, set<string> > data_map;

data_map["KEY1"].insert("VAL1");
data_map["KEY1"].insert("VAL2");

data_map["KEY2"].insert("VAL1");
data_map["KEY2"].insert("VAL3");

I get error here when i try to access set in map (inner for-cycle)

error: no match for call to ‘(std::set<std::basic_string<char> >) ()’|
error: no match for call to ‘(std::set<std::basic_string<char> >) ()’|


for( map<string, set<string> >::iterator mip = data_map.begin();mip != data_map.end(); ++mip) {
    for ( set<string>::iterator sit = mip->second().begin(); sit != mip->second().end(); ++sit )
        cout << *sit << endl;

}

Could you please tell me how i can iterate all values?

2
  • 4
    You just want second, not second() (it's a field of std::pair, not a method). Does that fix your issue? Commented Mar 11, 2014 at 13:48
  • @JeremyRoman I am sorry, you're right. Thank you! Commented Mar 11, 2014 at 13:49

3 Answers 3

3
mip->second().begin()

should be

mip->second.begin()
Sign up to request clarification or add additional context in comments.

Comments

2

You should use mip->second not mip->second(). I would recommed you to use auto in a for-each loop.

for(auto mip : data_map)
{  
    //Do another loop to get the values of your set, to get the set from the map use get<1>(mip);

}

Its better to read it that way and less space for errors.

1 Comment

If you're going to use auto, you're presumably in C++11. You might as well use the range-based for at that point.
0

Don't call the set like a function.

for( map<string, set<string> >::iterator mip = lines.begin();mip != lines.end(); ++mip) {
    for ( set<string>::iterator sit = mip->second.begin(); sit != mip->second.end();
          ++sit )
        cout << *sit << endl;

Comments

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.