result_of 用法

result_of 的用法:

(一)为什么使用result_of ?

  • 当我们不知道某个可调用对象(函数,std::funciton或者重载了operator()操作的对象)的返回类型的时候,使用其得到该可调用对象的返回类型

(二)调用格式

  • 包含头文件 < type_traits >
  • template<class Fn, class ArgType1, … ,class ArgTypen>
    class result_of<Fn(ArgType1, … , ArgType2)>::type
  • Fn : 可调用对象
  • ArgType : 可调用对象的参数

(三) 具体案例

  • 简单案例
#include <iostream>
#include <type_traits>
 
int fn(int) {return int();}                            // function
typedef int(&fn_ref)(int);                             // function reference
typedef int(*fn_ptr)(int);                             // function pointer
struct fn_class { int operator()(int i){return i;} };  // function-like class
 
int main() {
  typedef std::result_of<decltype(fn)&(int)>::type A;  // int
  typedef std::result_of<fn_ref(int)>::type B;         // int
  typedef std::result_of<fn_ptr(int)>::type C;         // int
  typedef std::result_of<fn_class(int)>::type D;       // int
 
  std::cout << std::boolalpha;
  std::cout << "typedefs of int:" << std::endl;
 
  std::cout << "A: " << std::is_same<int,A>::value << std::endl;
  std::cout << "B: " << std::is_same<int,B>::value << std::endl;
  std::cout << "C: " << std::is_same<int,C>::value << std::endl;
  std::cout << "D: " << std::is_same<int,D>::value << std::endl;
 
  return 0;
}
  • 输出结果:
  • typedefs of int:
    A: true
    B: true
    C: true
    D: true

一个模板中应用的实例:
  • 有一个vector,Person就是一个简单的结构体,包含name,age,city三个字段,想要编写一个Group_by函数,实现对这个vector按Person的某个字段分组
  • 如,按照age分组,拥有相同age的人放在同一组;若按照city分组,同样的相同城市的人将分在同一组
  • 最简单的方法是遍历一遍vector,并比较age或city的值进行分组;但这样会造成两段极其相似的代码,因为只有比较对象的类型不同(age int型或city string);
  • 因此,最好使用result_of 来得到比较对象的类型;
  • 思路是向Group_by传一个函数,让用户决定这个字段;分组比较简单,数据插入一个multimap<T,Person>返回即可,但是定义multimap中的T类型由用户传入的函数决定;于是这时候就可以用result_of来确定函数的返回值,即T的类型
#include<iostream>
#include<string>
#include<type_traits>
#include<vector>
#include<map>
#include<algorithm>
#include<utility>
using namespace std;

struct Person{
	int age;
	string city;
	string name;
};

vector<Person> person={
	{15, "chengdu", "Tim"},
	{24, "shanghai", "Xu"},
	{15, "Beijing", "Liu"},
	{20, "shanghai", "Li"}
	};
	
// Groupby function
template<typename Fn>
multimap<typename result_of<Fn(Person)>::type, Person> Groupby(const vector<Person> &person, const Fn  &keyselect)
{
	typedef typename result_of<Fn(Person)>::type key_type;
	multimap<key_type, Person> mymap;
	for_each(person.begin(),person.end(),[&](const Person &p{
		mymap.insert(make_pair(keyselect(p),p));
		});
	return mymap;
}

int main()
{   
    //按年龄分组
    auto res = GroupBy(person, [](const Person& p){ return p.age; });
    //按城市分组
    auto res1 = GroupBy(person, [](const Person& p) { return p.city; });
 
    //打印结果
    cout << "----------group by age:---------------" << endl;
    for_each(res.begin(),res.end(),[](decltype(res)::value_type & p){
                    cout << p.second.name <<" " << p.second.city << "  " << p.second.age  << endl;
                    });
    cout << "----------group by city:---------------"<< endl;
    for_each(res1.begin(),res1.end(),[](decltype(res1)::value_type & p){
                    cout << p.second.name <<" " << p.second.city << "  " << p.second.age  << endl;
                    });
    return 0;
}

参考:https://blog.csdn.net/qq_31175231/article/details/77165279

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值