C++ 从模板化容器类型获取元素类型
我正在尝试编写一个模板函数,该函数接受具有任意数字类型的任意容器类:C++ 从模板化容器类型获取元素类型,c++,templates,stl,C++,Templates,Stl,我正在尝试编写一个模板函数,该函数接受具有任意数字类型的任意容器类: template <typename NumType, typename ContType> double avg_nums(const ContType<NumType> & data) { double sum = 0; for(ContType::const_iterator iter = data.cbegin(); iter != data.cend(); ++ite
template <typename NumType, typename ContType>
double avg_nums(const ContType<NumType> & data)
{
double sum = 0;
for(ContType::const_iterator iter = data.cbegin(); iter != data.cend(); ++iter)
{
if(typeid(NumType) == typeid(char) || typeid(NumType) == typeid(unsigned char))
{
std::cout << static_cast<int>(*iter) << std::endl;
}
else
{
std::cout << *iter << " ";
}
sum += *iter;
}
std::cout << std::endl;
return sum / data.size();
}
模板
双平均值(常数类型和数据)
{
双和=0;
对于(ContType::const_迭代器iter=data.cbegin();iter!=data.cend();++iter)
{
if(typeid(NumType)==typeid(char)| | typeid(NumType)==typeid(无符号字符))
{
std::cout您可以使用以下表格:
template <typename ContType>
double avg_nums(ContType & data)
您可以使用以下表格:
template <typename ContType>
double avg_nums(ContType & data)
我认为我无论如何都不会实现该功能,因为您可以使用经过良好测试的现有算法:
int value = 0;
int total = std::accumulate(container.begin(), container.end(), value);
double avg = total * 1. / container.size();
我认为我无论如何都不会实现该功能,因为您可以使用经过良好测试的现有算法:
int value = 0;
int total = std::accumulate(container.begin(), container.end(), value);
double avg = total * 1. / container.size();
对于您当前的特定问题,您可以更换
template <typename NumType, typename ContType>
double avg_nums(const ContType<NumType> & data)
{
而不是
if(typeid(NumType) == typeid(char) || typeid(NumType) == typeid(unsigned char))
{
std::cout << static_cast<int>(*iter) << std::endl;
}
else
{
std::cout << *iter << " ";
}
if(typeid(NumType)==typeid(char)| | typeid(NumType)==typeid(unsigned char))
{
std::cout对于您当前的特定问题,您可以替换
template <typename NumType, typename ContType>
double avg_nums(const ContType<NumType> & data)
{
而不是
if(typeid(NumType) == typeid(char) || typeid(NumType) == typeid(unsigned char))
{
std::cout << static_cast<int>(*iter) << std::endl;
}
else
{
std::cout << *iter << " ";
}
if(typeid(NumType)==typeid(char)| | typeid(NumType)==typeid(unsigned char))
{
std::cout如果容器类型不具有某种“value_type”(这是首选方式,因为这依赖于作为第一个模板参数的值类型,而该参数不一定为true),则以下模板专用化变量可能也很有趣:
//定义一些没有某种“值类型”的自定义容器
模板类CustomContainer{};
//前向声明模板函数,以便我们可以在专门化中推断类型
模板双平均值(ContType&);
//从单个模板参数推断ContType和NumType的专门化
//这适用于第一个模板参数为“value\u type”的所有容器
//还要注意Ts…,它是可能需要传递的其他模板参数的占位符
样板
双平均值(ContType&)
{
//那么工作呢
返回NumType(0);
}
int main(int,char**)
{
海关集装箱控制;
平均值(续);
返回0;
}
如果容器类型不具有某种“值类型”(这是首选方式,因为这依赖于值类型作为第一个模板参数,但不一定为true),则以下模板专用化变量可能也很有趣:
//定义一些没有某种“值类型”的自定义容器
模板类CustomContainer{};
//前向声明模板函数,以便我们可以在专门化中推断类型
模板双平均值(ContType&);
//从单个模板参数推断ContType和NumType的专门化
//这适用于第一个模板参数为“value\u type”的所有容器
//还要注意Ts…,它是可能需要传递的其他模板参数的占位符
样板
双平均值(ContType&)
{
//那么工作呢
返回NumType(0);
}
int main(int,char**)
{
海关集装箱控制;
平均值(续);
返回0;
}
int value = 0;
int total = std::accumulate(container.begin(), container.end(), value);
double avg = total * 1. / container.size();
template <typename NumType, typename ContType>
double avg_nums(const ContType<NumType> & data)
{
template< class ContType >
double avg_nums(ContType const& data)
{
using NumType = typename ContType::value_type;
template< template< class, class > class ContType_, class NumType, class Allocator >
double avg_nums(ContType_<NumType, Allocator> const& data)
{
using ContType = ContType_<NumType, Allocator>;
if(typeid(NumType) == typeid(char) || typeid(NumType) == typeid(unsigned char))
{
std::cout << static_cast<int>(*iter) << std::endl;
}
else
{
std::cout << *iter << " ";
}
std::cout << +*iter << " ";
// define some custom container where we do not have some kind of 'value_type'
template <typename... T> class CustomContainer {};
// forward declare the template function so we can deduce the type in a specialization
template <typename ContType> double avg_nums(ContType&);
// specialization that deduces ContType and NumType from a single template argument
// This works for all continers where the first template argument is the 'value_type'
// also note the Ts... which is a placeholder for possible other template arguments that need to be passed on
template <template <typename...> class ContType, typename NumType, typename... Ts>
double avg_nums(ContType<NumType, Ts...>&)
{
// so the work
return NumType(0);
}
int main(int,char**)
{
CustomContainer<int> cont;
avg_nums(cont);
return 0;
}