定义如下:
//获取first
template<class T> struct select1st : public unary_function<const T, typename T::first_type>
{
typename T::first_type operator() (const T& x) {
return x.first;
}
};
//获取second
template<class T> struct select2nd : public unary_function<const T, typename T::second_type>
{
typename T::second_type operator() (const T& x) {
return x.second;
}
};
可以像如下方式使用:
int main(int argc, char* argv[])
{
map<int, int> mapInt;
mapInt[1] = 5;
mapInt[2] = 4;
mapInt[3] = 3;
mapInt[4] = 2;
mapInt[5] = 1;
list<int> listInt;
std::transform(mapInt.begin(), mapInt.end(), std::back_inserter(listInt), select2nd<map<int, int>::value_type>());
std::transform(mapInt.begin(), mapInt.end(), std::back_inserter(listInt), select1st<map<int, int>::value_type>());
system("pause");
return 0;
}