好的,所以我们知道 STL 中的函数,例如
std::fill(boolContainer.begin(), boolContainer.end(), false);
我正在使用一个也适用于容器的方法来开发一个类,并且我意识到我也可以像上面的示例中那样对其进行模板化。非模板化版本是这样的:
class SomeClass {
public:
// ...
int containerMethod(std::vector<int> &v);
// ...
private:
// ...
};
我的目标是将其更改为:
class SomeClass {
public:
// ...
template <class InputIterator>
int containerMethod(const InputIterator &begin, const InputIterator &end);
// ...
private:
// ...
};
但是我在制定实施细节时遇到了麻烦:
template <class Iter> int SomeClass::containerMethod
(const Iter &begin, const Iter&end) {
// Here I need to instantiate an iterator for the container.
Iter iter;
for (iter = begin; iter != end; ++iter) {
// This does not seem to work.
}
return 0;
}
所以问题是如何根据方法的模板化参数正确实例化模板化迭代器?请注意,我只需要一个输入迭代器。