一 定义:
1.头文件:<type_traits>
2.函数定义:
template< class >
class result_of;
// F为函数类型 ArgTypes为函数可变参数类型
template< class F, class... ArgTypes >
class result_of<F(ArgTypes...)>;
3.实例:
int fun(int x) {
return x + 1;
}
std::result_of<fun(int)>::type d = 10; // d的类型是fun返回类型int
二 应用实例:
1.问题描述:定义一个person数组,person包含姓名,年龄和城市信息,现在需要按照某一条件对该person数组做分组,条件可以是按照年龄或者按照姓名和城市等等。
2.分析:设计range类并提供group_by方法,该方法返回一个multimap,key为分组条件,value为person对象。
3.代码:
(1)range.hpp
#pragma once
#include <map>
#include <type_traits>
#include <algorithm>
template <typename R>
class range {
public:
typedef typename R::value_type value_type; // 获取R的类型
range(R &r) : range_(r) {
}
public:
template <typename KeyFn, typename ValueFn> // KeyFn为key对应的映射函数 即输入value_type类型的值 输出key ValueFn同理
std::multimap<typename std::result_of<KeyFn(value_type)>::type, typename std::result_of<ValueFn(value_type)>::type> // multimap的key和value都采用std::result_of获取类型
group_by(const KeyFn &keyf, const ValueFn &valuef) {
typedef typename std::result_of<KeyFn(value_type)>::type k_type;
typedef typename std::result_of<ValueFn(value_type)>::type v_type;
std::multimap<k_type, v_type>mm;
std::for_each(std::begin(range_), std::end(range_), [&mm, &keyf, &valuef] (const value_type &item) { // 这里要求rang_是容器类型
k_type key = keyf(item); // 映射出key
v_type value = valuef(item); // 映射出value
mm.insert(std::make_pair(key, value));
} );
return mm;
}
private:
R range_;
};
2.test.cpp
#include "range.hpp"
#include <iostream>
#include <string>
#include <vector>
#include <tuple>
struct person {
std::string name;
int age;
std::string city;
};
// 编译时递归打印tuple
template <typename Tuple, std::size_t N>
struct printer {
static void print(const Tuple &t) {
printer<Tuple, N - 1>::print(t);
std::cout << ", " << std::get<N - 1>(t);
}
};
template <typename Tuple>
struct printer<Tuple, 1> {
static void print(const Tuple &t) {
std::cout << std::get<0>(t);
}
};
//对printer函数进一步封装 支持可变参数
template <typename... Args>
void PrintTuple(const std::tuple<Args...> &t) {
std::cout << "(";
printer<decltype(t), sizeof...(Args)>::print(t);
std::cout << ")\n";
}
int main() {
typedef std::vector<person> R;
R vec{{"aa", 18, "bb"}, {"cc", 28, "dd"}, {"ahj", 12, "12dd"}};
range<R>r(vec);
// 第一种 按照姓名和年龄分组 向group传递lambda表达式
auto mm = r.group_by([](const person& _person) { return std::tie(_person.name, _person.age); },
[](const person& _person){ return std::tie(_person.city); }); // std::tie是形成tuple的工具 即将name和age组成一个tuple或者将city组成只有1个元素的tuple
for (auto &e: mm) {
std::cout << "first==============" << std::endl;
PrintTuple(e.first);
std::cout << "second==============" << std::endl;
PrintTuple(e.second);
}
// 第二种 按照年龄分组
auto mm1 = r.group_by([](const person& _person) { return std::tie(_person.age); },
[](const person& _person){ return std::tie(_person.name, _person.city); });
std::cout << "===========================" << std::endl;
for (auto &e: mm1) {
std::cout << "first==============" << std::endl;
PrintTuple(e.first);
std::cout << "second==============" << std::endl;
PrintTuple(e.second);
}
return 0;
}
https://github.com/wangzhicheng2013/result_of_multimap
4.运行结果