使用用户定义的类来计算某些类似的类模板
Calculate something with user defined classes by using variadic templates
坐标;
struct point{
int x_, y_;
}
假设我们具有上面定义的坐标。除了此结构外,我还想创建一个函数,该函数采用尽可能多的参数(例如,一个函数以找到三个或更多点的barycentre)。因此,我决定使用variadic模板。但是我认识到我缺乏有关变异模板的信息。
为了计算Barycentre,您应该添加所有X,将总和分为元素计数。并为y的元素做,然后返回点。您能带领我一种实施此操作的方法。
c 17的折叠表达使它变得轻而易举:
template <class... Points>
point barycentre(Points... points) {
point const sum{
(points.x_ + ...),
(points.y_ + ...)
};
int const num = sizeof...(Points);
return {
sum.x_ / num,
sum.y_ / num
};
}
看到它活在Coliru
如果您使用的是C 14,则可以简单地通过constexpr函数中的点迭代,例如这样(例如,从昆汀的答案中借来的例子):
#include <iostream>
struct point{
int x_, y_;
};
template <class... Points>
constexpr point barycentre(Points... points) {
point ps[] = {points...};
point result {0, 0};
for (std::size_t i = 0; i < sizeof...(points); i++) {
result.x_ += ps[i].x_;
result.y_ += ps[i].y_;
}
result.x_ /= sizeof...(points);
result.y_ /= sizeof...(points);
return result;
}
int main() {
constexpr auto bary = barycentre(point{0, 0}, point{2, 2});
std::cout << "(" << bary.x_ << ", " << bary.y_ << ")n";
}
[live demo]
和一些C 11:
#include <iostream>
#include <array>
#include <type_traits>
struct point{
int x_, y_;
};
template <std::size_t I, std::size_t N>
constexpr typename std::enable_if<I + 1 == N, point>::type barysum(std::array<point, N> const &points) {
return point{ points[I].x_, points[I].y_ };
}
template <std::size_t I, std::size_t N>
constexpr typename std::enable_if<I + 1 != N, point>::type barysum(std::array<point, N> const &points) {
return point{ points[I].x_ + barysum<I+1>(points).x_, points[I].y_ + barysum<I+1>(points).y_ };
}
template <std::size_t N>
constexpr point barycentre_impl(std::array<point, N> const points_array) {
return point{ barysum<0>(points_array).x_ / static_cast<int>(N), barysum<0>(points_array).y_ / static_cast<int>(N)};
}
template <class... Pts>
constexpr point barycentre(Pts... pts) {
return barycentre_impl<sizeof...(Pts)>( {{pts...}} );
}
int main() {
constexpr auto bary = barycentre(point{0, 0}, point{2, 2});
std::cout << "(" << bary.x_ << ", " << bary.y_ << ")n";
}
[live demo]
初始化列表更简单地使用相同类型的元素。
Point average(std::initializer_list<Point> list)
{
int x = 0, y = 0;
for(auto &i: list) {x += i.x_; y += i.y_;}
return {x / list.size(), y / list.size()};
}
variadic模板在 types 上是一般性的,因此它不适合您的情况。它不会使您的代码以任何方式更快地执行,只需膨胀二进制。
相关文章:
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 使用用户定义函数的字符串反转
- 用户定义函数中的指针和输入
- Visual C++(VS2017)中用户定义的转换不明确
- 使用用户定义的参数调用future/async并调用类方法
- 带有用户定义类的c++折叠表达式
- g++用户定义的动态链接库上的全局new和delete运算符
- 直接在 unordered_map 的方法中使用哈希,而不是生成哈希的用户定义对象
- 修改"std::set"中用户定义类型的值
- 参数包构造函数在类模板中隐藏用户定义的转换
- MAKE:找不到包含的用户定义的头文件?
- C++:用户定义的显式类型转换函数错误
- 从用户定义的类生成格式字符串?
- 为用户定义的类正确调用复制构造函数/赋值运算符
- C++ 向量与用户定义的类比较?(==, <, >)
- STL 用户定义的二进制操作
- 将用户定义的类型与 std::vector 和 std::sort 一起使用
- 为什么用户定义的函数不按照给定的顺序对相同长度的元素进行排序?
- 使用宏编译时使用用户定义的数学函数,或者仅使用 c++ 中标准数学库中的函数
- C++:用户定义的类,以成员字段作为地址