Continuing from my last question C++ template class map I have implemented the function to insert some values. This function inserts the same value for a range of keys. If the key exists in the map it should overwrite the old values. Is the function eventually correct and efficient? Could you suggest a better way to implement it?
void insert_ToMap( K const& keyBegin, K const& keyEnd, const V& value)
{
if(!(keyBegin < keyEnd))
return;
const_iterator it;
for(int j=keyBegin; j<keyEnd; j++)
{
it = my_map.find(j);
if(it==my_map.end())
{
my_map.insert(pair<K,V>(j,value));
}
else
{
my_map.erase(it);
my_map.insert(pair<K,V>(j, value));
}
}
}
I try:
int main()
{
template_map<int,int> Map1 (10);
Map1.insert_ToMap(3,6,20);
Map1.insert_ToMap(4,14,30);
Map1.insert_ToMap(34,37,12);
for (auto i = Map1.begin(); i != Map1.end(); i++)
{
cout<< i->first<<" "<<i->second<<std::endl;
}
}