initializer_lists 平均值的 C++11 通用函数
C++11 Generic Function for initializer_lists Averages
我在 C++ Primer 6th 中有一个练习:
Complete the program by supplying the average_list()
function. It should be a
template function, with the type parameter being used to specify the kind of
initialized_list
template to be used as the function parameter and also to give the function return type.
我不知道
这是一个小程序的一部分:
int main() {
using namespace std;
// list of double deduced from list contents
auto q = average_list({15.4, 10.7, 9.0});
cout << q << endl;
// list of int deduced from list contents
cout << average_list({20, 30, 19, 17, 45, 38} ) << endl;
// forced list of double
auto ad = average_list<double>({'A', 70, 65.33});
cout << ad << endl;
}
你可能会选择这样的东西:
#include <iterator>
#include <numeric>
#include <iostream>
template <typename T>
auto average_list(const std::initializer_list<T> &v) -> decltype(T() / 1.0)
{
return std::accumulate(std::begin(v), std::end(v), T()) / static_cast<float>(std::distance(std::begin(v), std::end(v)));
}
行
auto average_list(const std::initializer_list<T> &v) -> decltype(T() / 1.0)
表示 average_list
对某种类型 T
进行初始化列表 const 引用,returns 通过将 T
除以浮点数获得的类型。
函数体仅使用 numeric
等中的 STL 函数。
您需要编写一个参数化函数,它使用模板,使变量类型成为参数。
然后可以由编译器推断出传递值的类型,编译器专门化您的函数来处理传递的类型。
根据名称 average_list()
判断,该函数应该 return 传递的参数的平均值,因此您需要编写代码来做到这一点,例如,从一种类型开始int
然后简单地将 int
替换为 模板参数 。这样做的语法是:
template <typename T> // prefix in front of the function
示例:下面的函数只处理 int
int sum_vector(const std::vector<int>& v) {
int sum = 0;
for (size_t i = 0; i < v.size(); ++i){
// accumulate sum
sum += v[i];
}
return sum;
}
要确定上述函数参数的类型,可以这样写:
template<typename T> T sum_vector(const std::vector<T>& v) {
T sum = 0;
for (size_t i = 0; i < v.size(); ++i){
// accumulate sum
sum += v[i];
}
return sum;
}
然后T
取决于作为参数传递的vector
的类型,即如果向量的类型是int
,每个T
"becomes"1一个int
,如果double
那么每个T
"becomes"一个double
.
1.编译器为您提供类型为 int
.
的函数实例化
我在 C++ Primer 6th 中有一个练习:
Complete the program by supplying the
average_list()
function. It should be a template function, with the type parameter being used to specify the kind ofinitialized_list
template to be used as the function parameter and also to give the function return type.
我不知道
这是一个小程序的一部分:
int main() {
using namespace std;
// list of double deduced from list contents
auto q = average_list({15.4, 10.7, 9.0});
cout << q << endl;
// list of int deduced from list contents
cout << average_list({20, 30, 19, 17, 45, 38} ) << endl;
// forced list of double
auto ad = average_list<double>({'A', 70, 65.33});
cout << ad << endl;
}
你可能会选择这样的东西:
#include <iterator>
#include <numeric>
#include <iostream>
template <typename T>
auto average_list(const std::initializer_list<T> &v) -> decltype(T() / 1.0)
{
return std::accumulate(std::begin(v), std::end(v), T()) / static_cast<float>(std::distance(std::begin(v), std::end(v)));
}
行
auto average_list(const std::initializer_list<T> &v) -> decltype(T() / 1.0)
表示 average_list
对某种类型 T
进行初始化列表 const 引用,returns 通过将 T
除以浮点数获得的类型。
函数体仅使用 numeric
等中的 STL 函数。
您需要编写一个参数化函数,它使用模板,使变量类型成为参数。
然后可以由编译器推断出传递值的类型,编译器专门化您的函数来处理传递的类型。
根据名称 average_list()
判断,该函数应该 return 传递的参数的平均值,因此您需要编写代码来做到这一点,例如,从一种类型开始int
然后简单地将 int
替换为 模板参数 。这样做的语法是:
template <typename T> // prefix in front of the function
示例:下面的函数只处理 int
int sum_vector(const std::vector<int>& v) {
int sum = 0;
for (size_t i = 0; i < v.size(); ++i){
// accumulate sum
sum += v[i];
}
return sum;
}
要确定上述函数参数的类型,可以这样写:
template<typename T> T sum_vector(const std::vector<T>& v) {
T sum = 0;
for (size_t i = 0; i < v.size(); ++i){
// accumulate sum
sum += v[i];
}
return sum;
}
然后T
取决于作为参数传递的vector
的类型,即如果向量的类型是int
,每个T
"becomes"1一个int
,如果double
那么每个T
"becomes"一个double
.
1.编译器为您提供类型为 int
.