S
Senthilvel
Hi ,
I am trying out a few templates and i got stuck in template specialization.
The normal template Add(just like a plus of the fubnctional) works fine.
But i wanted to specialize it for a map so that the value passed would be
added to the
mapped_value of each pair in the map.But the program fails to compile.
Can you kindly point out my error.
The source code is given below.
Thnaks and Best Regards,
Senthilvel.
#pragma warning(disable: 4786)
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std:stream;
#include <map>
using std::map;
using std:air;
#include <vector>
using std::vector;
#include <algorithm>
using std::copy;
using std::transform;
#include <string>
using std::string;
#include <iterator>
using std:stream_iterator;
template<class T,class U > ostream& operator << (ostream& os,const
pair<T,U>& val)
{
os<<"Key: " <<val.first<<endl;
os<<"Mapped Value: " << val.second<<endl;
return os;
}
template<class T,class U = T> struct Add:unary_function<T,U>
{
private:
T value_;
public:
Add(const T& val):value_(val){}
U operator()(const T& val)
{
return val + value_;
}
};
template<class T,class U> struct Add< pair<T,U> >:unary_function<
pair<T,U>,pair<T,U> >
{
private:
U& value_;
public:
Add(const U& val):value_(val){}
pair<T,U> operator()(const pair<T,U> & someVal)
{
U v = someVal.second + value_;
return make_pair(someVal.first,v);
}
};
int main()
{
/***********this block works fine ********************/
vector<int> myVec;
myVec.push_back(100);
myVec.push_back(100);
myVec.push_back(100);
myVec.push_back(100);
myVec.push_back(100);
copy(myVec.begin(),myVec.end(),ostream_iterator<int>(cout));
cout<<'\n';
transform(myVec.begin(),myVec.end(),myVec.begin(),Add<int>(2));
copy(myVec.begin(),myVec.end(),ostream_iterator<int >(cout));
cout<<'\n';
/************************************************/
map<int,string> myMap;
myMap[0] = "Senthil";
myMap[1] = "Sreeni";
myMap[2] = "Chandy";
myMap[3] = "Satish";
myMap[4] = "Taruna";
while( !cin.get() );
return 0;
}
I am trying out a few templates and i got stuck in template specialization.
The normal template Add(just like a plus of the fubnctional) works fine.
But i wanted to specialize it for a map so that the value passed would be
added to the
mapped_value of each pair in the map.But the program fails to compile.
Can you kindly point out my error.
The source code is given below.
Thnaks and Best Regards,
Senthilvel.
#pragma warning(disable: 4786)
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std:stream;
#include <map>
using std::map;
using std:air;
#include <vector>
using std::vector;
#include <algorithm>
using std::copy;
using std::transform;
#include <string>
using std::string;
#include <iterator>
using std:stream_iterator;
template<class T,class U > ostream& operator << (ostream& os,const
pair<T,U>& val)
{
os<<"Key: " <<val.first<<endl;
os<<"Mapped Value: " << val.second<<endl;
return os;
}
template<class T,class U = T> struct Add:unary_function<T,U>
{
private:
T value_;
public:
Add(const T& val):value_(val){}
U operator()(const T& val)
{
return val + value_;
}
};
template<class T,class U> struct Add< pair<T,U> >:unary_function<
pair<T,U>,pair<T,U> >
{
private:
U& value_;
public:
Add(const U& val):value_(val){}
pair<T,U> operator()(const pair<T,U> & someVal)
{
U v = someVal.second + value_;
return make_pair(someVal.first,v);
}
};
int main()
{
/***********this block works fine ********************/
vector<int> myVec;
myVec.push_back(100);
myVec.push_back(100);
myVec.push_back(100);
myVec.push_back(100);
myVec.push_back(100);
copy(myVec.begin(),myVec.end(),ostream_iterator<int>(cout));
cout<<'\n';
transform(myVec.begin(),myVec.end(),myVec.begin(),Add<int>(2));
copy(myVec.begin(),myVec.end(),ostream_iterator<int >(cout));
cout<<'\n';
/************************************************/
map<int,string> myMap;
myMap[0] = "Senthil";
myMap[1] = "Sreeni";
myMap[2] = "Chandy";
myMap[3] = "Satish";
myMap[4] = "Taruna";
copy(myMap.begin() said:(cout));
transform(myMap.begin() said:("HaHaHa")); >>>>>>>>>>>>>Error here
copy(myMap.begin() said:(cout));
while( !cin.get() );
return 0;
}