Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid duplicate values in maps using c++

Tags:

c++

c++11

I am trying to write a program in C++ using maps...

My goal is to avoid the same values repeated in maps.

If the keys are same, we can use maps to avoid the duplicated keys. To allow duplicate keys, we use multimaps.

In case the value is the same, how can we avoid that?

The program which I have written allows duplicated values:

typedef std::map<int, std::string> MyMap;

int main()
{
    MyMap map;
    MyMap::iterator mpIter;

    int key;
    string value;

    int count;
    for(count = 0; count < 3;count++)
    {
        cin >> key;
        cin >> value;
               
        std::pair<MyMap::iterator, bool> res = map.insert(std::make_pair(key,value));
    }

    for (mpIter=map.begin(); mpIter != map.end(); ++mpIter)
         cout  << " "  << (*mpIter).second << endl;
}
like image 841
udaya chandran Avatar asked Apr 30 '16 18:04

udaya chandran


People also ask

Does map take duplicate values?

Map does not supports duplicate keys. you can use collection as value against same key. Because if the map previously contained a mapping for the key, the old value is replaced by the specified value.

Does std :: map allow duplicates?

Allows Duplicates: Even can exist in unordered_multimap twice.

What happens if we enter duplicate key in map?

If you try to insert the duplicate key, it will replace the element of the corresponding key. HashMap is similar to HashTable, but it is unsynchronized. It allows to store the null keys as well, but there should be only one null key object and there can be any number of null values.


1 Answers

Make the value part of the key and/or use a set but that may not really solve the problem. It isn't possible to easily define a container that has both unique keys AND values if that's what you want. However, you might still construct one. Here's a very simple example to illustrate what is needed:

// Assuming keys are KEY and values are VAL

class MyMap  {
public:
   std::set<KEY> keyset;
   std::set<VAL> valset;

   std::map<KEY,VAL> theRealMap;

   // assuming existence of  function HAS(S,V) 
   // which returns true if v is in set S
   bool MyInsert(KEY ky, VAL val) {
       if (HAS(keyset,  ky) return false;
       if (HAS(valset, val) return false;
       keyset.insert(ky);
       valset.insert(vl);
       return theRealMap.insert(std::pair<KEY,VAL>(ky, val));
   }
:
:

Since this is an example it's not intended to be copied. You will likely want to include the functionality provided by std:map. An easy way would be to use std::map as a base class but you will need to hide (by making private) or implement similar code for each variant of insert otherwise you might get inadvertent insert that may not be unique.

Note: this requires twice the size of a single map. You can save some space by using theRealMap instead of a separate set for keys set. Another way would be to search the map but that sacrifices time for space. It's your call.

like image 113
DAV Avatar answered Sep 21 '22 14:09

DAV