简体   繁体   中英

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?

mip->second().begin()

应该

mip->second.begin()

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM