C++ 根据值类型调用适当的构造函数:整型或浮点型
我有一个函数,它用均匀分布的最小值和最大值之间的随机值填充容器C++ 根据值类型调用适当的构造函数:整型或浮点型,c++,templates,c++11,C++,Templates,C++11,我有一个函数,它用均匀分布的最小值和最大值之间的随机值填充容器 #include <iostream> #include <random> #include <algorithm> #include <vector> template<typename TContainer> void uniform_random(TContainer& container, const typename TContainer::value
#include <iostream>
#include <random>
#include <algorithm>
#include <vector>
template<typename TContainer>
void uniform_random(TContainer& container,
const typename TContainer::value_type min,
const typename TContainer::value_type max) {
std::random_device rd;
std::mt19937 gen(rd());
// Below line does not work with integers container
std::uniform_real_distribution<typename TContainer::value_type> distribution(min, max);
auto lambda_norm_dist = [&](){ return distribution(gen); };
std::generate(container.begin(), container.end(), lambda_norm_dist);
}
int main() {
std::vector<float> a(10);
uniform_random(a,0,10);
for (auto el : a) { std::cout << el << " "; }
}
#包括
#包括
#包括
#包括
模板
空隙均匀随机(t容器和容器,
const typename TContainer::value\u type min,
常量typename TContainer::value\u type max){
std::随机_装置rd;
标准:mt19937 gen(rd());
//下面的行不适用于整数容器
标准:均匀实分布(最小值、最大值);
自动lambda_norm_dist=[&](){返回分布(gen);};
std::generate(container.begin()、container.end()、lambda\u norm\u dist);
}
int main(){
std::载体a(10);
均匀随机(a,0,10);
对于(auto el:a){std::cout编写一个元函数select_distribution
,它允许您编写以下内容:
using value_type = typename TContainer::value_type;
using distribution_type = typename select_distribution<value_type>::type;
distribution_type distribution(min, max);
希望能有所帮助。一个解决方案是使用类型特征帮助器和std::enable_如果
:
template<class T, class Enable = void>
struct uniform_distribution_helper {};
template<class T>
struct uniform_distribution_helper<T, typename std::enable_if<std::is_floating_point<T>::value >::type> {
using type = std::uniform_real_distribution<T>;
};
template<class T>
struct uniform_distribution_helper<T, typename std::enable_if<std::is_integral<T>::value >::type> {
using type = std::uniform_int_distribution<T>;
};
模板
结构一致分布{};
模板
结构一致分布辅助对象{
使用type=std::均匀实分布;
};
模板
结构一致分布辅助对象{
使用type=std::均匀分布;
};
然后在您的函数中:
using uniform_distribution = typename uniform_distribution_helper<typename TContainer::value_type>::type;
// Below line does not work with integers container
uniform_distribution distribution(min, max);
使用统一分布=typename统一分布\u helper::type;
//下面的行不适用于整数容器
均匀分布(最小值、最大值);
尝试使用traits类。例如:
template <typename NumType>
struct ValueTraits;
template <>
struct ValueTraits<int>
{
using UniformDistributionType = std::uniform_int_distribution<int>;
};
在C++14(或C++11,稍作修改)中,您可以通过以下方式创建统一分布
类型别名:
template <typename ValueType>
using uniform_distribution = std::conditional_t<
std::is_floating_point<ValueType>::value,
std::uniform_real_distribution<ValueType>,
std::uniform_int_distribution<ValueType>
>;
模板
使用统一分布=标准::条件分布<
std::is_floating_point::value,
标准::均匀实分布,
标准:均匀分布
>;
用法:
uniform_distribution<typename TContainer::value_type> distribution(min, max);
均匀分布(最小值、最大值);
使用适当的源代码高亮显示(反勾号)。如果用简单的强调符号(星号)括起代码,则不会显示尖括号。(已修复。)template using select\u distribution=std::conditional\u t;
@SebastianRedl:我认为这行不通,因为这两个分支都需要编译,但它们是互斥的!std::uniform\u real\u distribution
只要您不需要类的定义就可以编译,除非标准库做得非常复杂和非标准防止这种情况发生的措施。@SebastianRedl:如果是这样的话,那就最好了。我曾想过使用它,但拒绝了它,因为std::conditional
得到了热切的评价。谢谢你!投票选了这个,我个人觉得它更直观。谢谢你的启发!我尝试过使用std::enable\u If
,但没有成功。
template <typename ValueType>
using uniform_distribution = std::conditional_t<
std::is_floating_point<ValueType>::value,
std::uniform_real_distribution<ValueType>,
std::uniform_int_distribution<ValueType>
>;
uniform_distribution<typename TContainer::value_type> distribution(min, max);