我想要一个接口ModelGenerator,它有一个方法generate(),它接受一个可迭代的Evidence列表并创建一个Model.使用STL伪鸭输入迭代器成语...
template<class Model>
class ModelGenerator {
public:
template<class Iterator>
virtual bool generate(Iterator begin, Iterator end, Model& model) = 0;
};
Run Code Online (Sandbox Code Playgroud)
但虚函数无法模板化.所以我必须模拟整个班级:
template<class Model, class Iterator>
class ModelGenerator {
public:
virtual bool generate(Iterator begin, Iterator end, Model& model) = 0;
};
Run Code Online (Sandbox Code Playgroud)
理想情况下,我想做的是这样的......
template<class Model, class Evidence>
class ModelGenerator {
public:
virtual bool generate(iterator<Evidence>& begin,
iterator<Evidence>& end,
Model& model) = 0;
};
Run Code Online (Sandbox Code Playgroud)
但迭代器没有继承这样的接口.(类std :: iterator只包含一堆typedef,没有方法.)
我能想到的唯一方法是给ModelGenerator一个方法addEvidence(),它在调用generate()之前逐个添加它们,但是我必须给出一个有点痛苦的ModelGenerator状态.
如何编写一个带有任何STL容器的虚方法?
我需要遍历图形的边缘并检查每条边的权重.我没有修改边缘,因此我的函数对图形采用const引用.但是,我知道获得边权重的唯一方法是访问属性映射,这似乎违反了const-ness.
void printEdgeWeights(const Graph& graph) {
typedef Graph::edge_iterator EdgeIterator;
std::pair<EdgeIterator, EdgeIterator> edges = boost::edges(graph);
typedef boost::property_map<Graph, boost::edge_weight_t>::type WeightMap;
// The following line will not compile:
WeightMap weights = boost::get(boost::edge_weight_t(), graph);
EdgeIterator edge;
for (edge = edges.first; edge != edges.second; ++edge) {
std::cout << boost::get(weights, *edge) << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
所以我必须这样做:
Graph& trust_me = const_cast<Graph&>(graph);
WeightMap weights = boost::get(boost::edge_weight_t(), trust_me);
Run Code Online (Sandbox Code Playgroud)
有办法避免这种情况吗?
另一方面,属性映射查找是否是恒定时间?
作为参考,这是我对Graph的定义.
struct FeatureIndex { ... };
typedef boost::property<boost::vertex_index_t, int,
FeatureIndex>
VertexProperty;
typedef boost::property<boost::edge_index_t, int,
boost::property<boost::edge_weight_t, int> >
EdgeProperty;
typedef …Run Code Online (Sandbox Code Playgroud)