我有std::vector<double>
, 和我的类型Array
。此外, 中有很多数字std::vector<double> source
,我想让这段代码以这种方式工作:
std::cout << toHisto(source, /* other parameters like min, max, stacks */);
我猜如果该函数toHisto
与其他容器一起使用,例如我的Array
. 你,我试图这样做,并且模板函数的返回类型有问题Error C3200 'Container<double,std::allocator<double>>': invalid template argument for template parameter 'Container', expected a class template
如果您有任何建议,我将很高兴听到。
代码在这里:
template<template <class T, class Allocator = std::allocator<T>> class Container>
class MyClassForOstream {
public:
Container<double> &res;
double min;
double max;
MyClassForOstream(Container<double> r, double min_, double max_) : res(r), min(min_), max(max_) {}
friend std::ostream& operator<< (std::ostream& out, MyClassForOstream const& s) {
/* sth like for(i in s)"out << s[i]" , and some format stuff from <iomanip>*/
return out;
}
};
template<template <class T, class Allocator = std::allocator<T>> class Container>
// Error here at specifing params for MyClassForOstream < /* here */>
MyClassForOstream<Container<double>> toHisto(Container<double> const& gen, double min, double max, size_t stocks) { // class template must have: operator[], .size(), ctor(size)
Container<size_t> tmp(stocks);
for (size_t i = 0; i != stocks; ++i) {
tmp[i] = 0;
}
for (size_t i = 0; i != gen.size(); ++i) {
++tmp[static_cast<size_t>(((min + gen[i]/(max+min))*((max-min)*stocks)))];
}
Container<double> res(stocks);
for (size_t i = 0; i != res.size(); ++i) {
res[i] = 0;
}
for (size_t i = 0; i != res.size(); ++i) {
res[i] = static_cast<double>(tmp[i])/gen.size();
}
return MyClassForOstream<Container<double>> (res, min, max ); // also error here
}```