5

I need advice how to insert data into map with string and set<string>. I tried something like this, but it doesnt work:

#include <map>
#include <utility>

int main()
{      
  std::map<string, set<string> > mymap;
  std::map<string, set<string> >::iterator it = mymap.begin(); 

  mymap.insert ( std::pair<string, set<string> > ("car" , "orange") );
  return (0);
}

Could someone help me? Thank you in advance.

2
  • mymap[key] = value;? Otherwise looks like the second part of your pair is not a set. Commented Mar 14, 2014 at 21:51
  • in C++11, mymap.insert({"car", {"orange"}}); should work. The problem with the code in the question is that "orange" is not a set. Commented Mar 14, 2014 at 22:12

4 Answers 4

10

I don't know why it seems so popular these days to avoid operator[] for maps, it's so much more elegant than insert. Unless of course, the slight differences (such as the need for a default constructor) cause it to not work for you, or you've determined that it is causing a performance problem. For your purposes, as you've described them, it should work fine.

mymap["car"].insert("orange");

This will construct the set if it doesn't already exist, and it will insert an element into it. In comments, you've expressed a desire to add further elements to the set, and you can use the exact same syntax.

mymap["car"].insert("blue");

If you want to ensure you have a fresh, empty set before inserting, you can always call clear() on it.

auto & car_set = mymap["car"];
car_set.clear();
car_set.insert("orange");
Sign up to request clarification or add additional context in comments.

1 Comment

This is as elegant as it can get, but I were to use insert instead of operator [] , how can it be done if the key is already present in the map?
6

Two methods:

set<string> myset;
myset.insert("orange");

//first method
mymap["car"] = myset; //will overwrite existing data!!

//second method
mymap.insert(make_pair("car", myset));

3 Comments

these methods are not equivalent
@lizusek, would you please elaborate? Thanks.
@RSahu if key is already present in map, insert will not insert, but [] will overwrite, so basically answer given above is not correct since question was about how to 'insert'
4
#include <map>
#include <set>
#include <string>

int main() {
    std::map< std::string, std::set< std::string> > mymap;
    std::set< std::string> s;
    s.insert( "orange");
    mymap.insert( std::make_pair( std::string("car") , s));
    return 0;
}

To add new element to existing std::set:

//add string to set identified by key "car"
if ( mymap.find( "car") != mymap.end()) {
    std::set< std::string>& s_ref = mymap[ "car"];
    s_ref.insert( "blue");
}

Please check this online example.

2 Comments

Thank you for your answers, it helps me, but I have one more question. If I want to add next string to set? This doesnt work :/ pastebin.com/aD3NGNAd
@user3421673 please take a look at the updated answer
3
#include <map> 
#include <set>
#include <string>
#include <iostream>

using namespace std;

void insertValue(map<string, set<string> >& myMap,
                 string const& key,
                 string const& value)
{
   // Check whether there is already a set given the key.
   // If so, insert to the existing set.
   // Otherwise, create a set and add it to the map.
   map<string, set<string> >::iterator found = myMap.find(key);
   if ( found != myMap.end() )
   {
      cout << "Adding '" << value << "' to an existing set of " << key << "s.\n";
      found->second.insert(value);
   }
   else
   {
      cout << "Adding '" << value << "' to a new set of " << key << "s.\n";
      set<string> temp;
      temp.insert(value);
      myMap.insert(make_pair(key, temp));
   }
}

int main()
{
   map<string, set<string> > mymap;
   insertValue(mymap, "car", "orange");
   insertValue(mymap, "car", "blue");
   insertValue(mymap, "car", "red");
   insertValue(mymap, "truck", "orange");
   insertValue(mymap, "truck", "blue");
   return 0;
}

1 Comment

Other than the output statements, your insertValue function achieves the exact same thing as simply doing: mymap[key].insert(value);

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.