samedi 18 avril 2020

How to output a Map with keys of type string and the values are the set of strings

I"m writing a method called put() that is suppose to output a map with the correct values. My printing method works but my actual method doesn't make the map correctly. The keys are strings and the values of the map is a set of strings. My method is also of type T to work with other values but its made for strings.

I'm trying to use a nest for each loop to grab the input set and use it for the keys in the map. The next for each loop is iterate through the set and insert the the values at the corresponding keys if the key is not equal too the value. That's where my problem is occurring but I'm not sure how to fix it.

input: set = {"a", "b", "c", "d"}

output: expected --vs-- actual

Key: a Value: b c d --- Key: a Value: b c d

Key: b Value: a c d --- Key: b Value: a b c d

Key: c Value: a b d --- Key: c Value: a b c d

and so on...

void
  put (const Container& c)
  {
    std::map<T, std::set<T>> map;
    std::set<T> set;
    for(auto i : c)
    {
      for(auto j : c)
      {
        if(i != j)   //this is where I think I am wrong
        {
         set.insert(j);
         map[i] = set;
        }      
      }
    }
     //my printing method is here but I left it out for simpler code
  }

int
  main ()
  {
    set<string> v = {"a", "b", "c", "d"};
    manager.put(v);
  }

Aucun commentaire:

Enregistrer un commentaire