template<class T>
map<T,int> counter(vector<T> a)
{
map<T,int> b;
pair<map<T,int>::iterator ,bool> pos; //This line shows compiler message
for(int i=0;i<a.size();++i)
{
pos=b.insert(make_pair(a[i],1));
if(!(pos.second))
{
b[pos.first -> first]++;
}
}
return b;
}
This is code using templates.
map<int,int> counter(vector<int> a)
{
map<int,int> b;
pair<map<int,int>::iterator ,bool> pos;
for(int i=0;i<a.size();++i)
{
pos=b.insert(make_pair(a[i],1));
if(!(pos.second))
{
b[pos.first -> first]++;
}
}
return b;
}
This is the function if I rewrite for integers alone. Integers code is working fine and first one is not. Can you explain me why it is happening and How can I modify it so I works for every data type?