samedi 30 avril 2016

How to avoid duplicate values in maps using c++

I am trying to write a program in cpp using maps...

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

if the key is same ,we can use maps to avoid the duplicates keys.To allow duplicate keys ,we use multimaps

Incase if the value is same,how we can avoid ?

The program which i have written allows duplicate 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;
}

Aucun commentaire:

Enregistrer un commentaire