Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C+中的通用STL兼容直方图+;11 这是我在C++中用GCC 4.6测试的通用直方图模板函数的第一次尝试。但是,我想将密集直方图()和稀疏直方图()合并到一个通用函数模板中。问题是密集特定构造函数hh(n,0)在稀疏版本hh中既没有定义也没有相关性。有没有办法用一些聪明的C++方式来解决这个问题,或者静态地使用Boost类型的条件编译(Mulk? #包括 #包括 #包括 #包括 #包括 名称空间标准 { /*! *\em \p a的密集直方图。 * *\t RAM V是值类型。 *\t内存C是计数(Bin)类型。 *\t直方图H是直方图存储类型,通常为矢量。 * *\param[in]x是一组输入数据集 */ 模板 内联 H密集直方图(常数V&x) { typedef typename V::value_type E;//元素类型 size\u t n=(static\u cast(1))_C++_Templates_Generics_Histogram_Conditional Compilation - Fatal编程技术网

C+中的通用STL兼容直方图+;11 这是我在C++中用GCC 4.6测试的通用直方图模板函数的第一次尝试。但是,我想将密集直方图()和稀疏直方图()合并到一个通用函数模板中。问题是密集特定构造函数hh(n,0)在稀疏版本hh中既没有定义也没有相关性。有没有办法用一些聪明的C++方式来解决这个问题,或者静态地使用Boost类型的条件编译(Mulk? #包括 #包括 #包括 #包括 #包括 名称空间标准 { /*! *\em \p a的密集直方图。 * *\t RAM V是值类型。 *\t内存C是计数(Bin)类型。 *\t直方图H是直方图存储类型,通常为矢量。 * *\param[in]x是一组输入数据集 */ 模板 内联 H密集直方图(常数V&x) { typedef typename V::value_type E;//元素类型 size\u t n=(static\u cast(1))

C+中的通用STL兼容直方图+;11 这是我在C++中用GCC 4.6测试的通用直方图模板函数的第一次尝试。但是,我想将密集直方图()和稀疏直方图()合并到一个通用函数模板中。问题是密集特定构造函数hh(n,0)在稀疏版本hh中既没有定义也没有相关性。有没有办法用一些聪明的C++方式来解决这个问题,或者静态地使用Boost类型的条件编译(Mulk? #包括 #包括 #包括 #包括 #包括 名称空间标准 { /*! *\em \p a的密集直方图。 * *\t RAM V是值类型。 *\t内存C是计数(Bin)类型。 *\t直方图H是直方图存储类型,通常为矢量。 * *\param[in]x是一组输入数据集 */ 模板 内联 H密集直方图(常数V&x) { typedef typename V::value_type E;//元素类型 size\u t n=(static\u cast(1)),c++,templates,generics,histogram,conditional-compilation,C++,Templates,Generics,Histogram,Conditional Compilation,我认为您只需将公共部分放在第三个函数中,留下密集直方图和稀疏直方图来创建h,并调用该实现函数: template <class V, class C = size_t, class H> inline void histogram_impl(const V & x, H& h) { typedef typename V::value_type E; // element type C bmax = 0; //

我认为您只需将公共部分放在第三个函数中,留下
密集直方图
稀疏直方图
来创建
h
,并调用该实现函数:

template <class V, class C = size_t, class H>
inline void histogram_impl(const V & x, H& h) {
    typedef typename V::value_type E; // element type
    C bmax = 0;                      // bin max
    for_each(begin(x), end(x),  // C++11
             [&h, &bmax] (const E & e) { // value element
                 h[e]++;
                 bmax = std::max(bmax, h[e]);
             });
    return h;
}
template <class V, class C = size_t, class H = vector<C> >
inline H dense_histogram(const V & x) {
    typedef typename V::value_type E; // element type
    size_t n = (static_cast<C>(1)) << (8*sizeof(E)); // maximum number of possible elements for dense variant
    H h(n, 0);                       // histogram
    histogram_impl(x, h);
    return h;
}
template <class V, class C = size_t, class H = unordered_map<typename V::value_type, C> >
inline H sparse_histogram(const V & x) {
    H h;                        // histogram
    histogram_impl(x, h);
    return h;
}
模板
内联空心柱状图(常数V&x、H&H){
typedef typename V::value_type E;//元素类型
C bmax=0;//bin max
对于每个(开始(x),结束(x),//C++11
[&h,&bmax](常量E&E){//value元素
h[e]++;
bmax=std::max(bmax,h[e]);
});
返回h;
}
模板
内联H密集_直方图(常数V&x){
typedef typename V::value_type E;//元素类型

size\u t n=(static\u cast(1))您不能用稠密映射实现稀疏映射吗?您所做的只是切换容器的默认模板参数。我只需要一个create\u histogram函数,模板化在容器上。好的,我将更改名称空间。
template <class V, class C = size_t, class H>
inline void histogram_impl(const V & x, H& h) {
    typedef typename V::value_type E; // element type
    C bmax = 0;                      // bin max
    for_each(begin(x), end(x),  // C++11
             [&h, &bmax] (const E & e) { // value element
                 h[e]++;
                 bmax = std::max(bmax, h[e]);
             });
    return h;
}
template <class V, class C = size_t, class H = vector<C> >
inline H dense_histogram(const V & x) {
    typedef typename V::value_type E; // element type
    size_t n = (static_cast<C>(1)) << (8*sizeof(E)); // maximum number of possible elements for dense variant
    H h(n, 0);                       // histogram
    histogram_impl(x, h);
    return h;
}
template <class V, class C = size_t, class H = unordered_map<typename V::value_type, C> >
inline H sparse_histogram(const V & x) {
    H h;                        // histogram
    histogram_impl(x, h);
    return h;
}
template<typename T> struct has_explicit_length_constructor{
private:
   template<typename U>
   decltype(U(0, 0), void(), std::true_type()) test(int x);
   template<typename>
   std::false_type test(...);
  typedef decltype(test<T>(0)) constant_type;
public:
   constexpr bool value = constant_type::value;
};

template<class H, bool B = has_explicit_length_constructor<H>::value> struct histogram_creation_trait;
template<class H> struct histogram_creation_trait<H, true> {
  static H create()  {
    size_t n = (static_cast<C>(1)) << (8*sizeof(typename V::value_type));
    return H(n, 0);  
  }
};
template<class H> struct histogram_creation_trait<H, false>
{ static H create()  { return H(); } };

template <class V, class C = size_t, class Ht>
inline void histogram_impl(const V & x, H& h, Trait) {
    typedef typename V::value_type E; // element type
    C bmax = 0;                      // bin max
    H h = histogram_creation_trait<H>::create();
    for_each(begin(x), end(x),  // C++11
             [&h, &bmax] (const E & e) { // value element
                 h[e]++;
                 bmax = std::max(bmax, h[e]);
             });
    return h;
}
template <class V, class H = vector<size_t> > H make_dense_histogram(const V & x) { return histogram_impl<V, size_t, H>(x); }
template <class V, class H = unordered_map<typename V::value_type, size_t> > H make_sparse_histogram(const V & x) { return histogram_impl<V, size_t, H>(x); }