#include <iostream>
#include <string>
#include <map>
bool insert( const std::string &key , const int value , std::map<std::string , int> &m );
int main()
{
// your code goes here
std::map<std::string,int> m;
m["apple"] = 1;
m["grape"] = 2;
insert( "orange" , 1 , m );
insert( "apple" , 3 , m );
insert( "orange" , 3 , m );
for( const auto &it : m )
std::cout << it.first << " = " << it.second << std::endl;
return 0;
}
bool insert( const std::string &key , const int value , std::map<std::string , int> &m )
{
bool found = false;
if( m.find(key) != m.end() )
{
found = true;
}
else
{
for( const auto &it : m ) //for( std::map<std::string,int>::iterator it = m.begin() , it != m.end(); ++it )
{
if( it.second == value )
{
found = true;
}
}
}
if( !found )
{
m[key] = value;
std::cout << "The item was added" << std::endl;
}
else
{
std::cout << "The item was not added" << std::endl;
}
return( found );
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8bWFwPgoKYm9vbCBpbnNlcnQoIGNvbnN0IHN0ZDo6c3RyaW5nICZrZXkgLCBjb25zdCBpbnQgdmFsdWUgLCBzdGQ6Om1hcDxzdGQ6OnN0cmluZyAsIGludD4gJm0gKTsKCmludCBtYWluKCkKewoJLy8geW91ciBjb2RlIGdvZXMgaGVyZQoJc3RkOjptYXA8c3RkOjpzdHJpbmcsaW50PiBtOwoJbVsiYXBwbGUiXSA9IDE7CgltWyJncmFwZSJdID0gMjsKCQoJaW5zZXJ0KCAib3JhbmdlIiAsIDEgLCBtICk7CgkJCglpbnNlcnQoICJhcHBsZSIgLCAzICwgbSApOwoJCQoJaW5zZXJ0KCAib3JhbmdlIiAsIDMgLCBtICk7CgkJCglmb3IoIGNvbnN0IGF1dG8gJml0IDogbSApCgkJc3RkOjpjb3V0IDw8IGl0LmZpcnN0IDw8ICIgPSAiIDw8IGl0LnNlY29uZCA8PCBzdGQ6OmVuZGw7CglyZXR1cm4gMDsKfQoKYm9vbCBpbnNlcnQoIGNvbnN0IHN0ZDo6c3RyaW5nICZrZXkgLCBjb25zdCBpbnQgdmFs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