Rob
Rob

Reputation: 718

C++ Using std::set<> in std::map<>

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?

Upvotes: 2

Views: 293

Answers (3)

GreenOnBlack
GreenOnBlack

Reputation: 66

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.

Upvotes: 2

Fred Larson
Fred Larson

Reputation: 62123

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;

Upvotes: 0

Sebastian Hoffmann
Sebastian Hoffmann

Reputation: 11502

mip->second().begin()

should be

mip->second.begin()

Upvotes: 3

Related Questions