Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/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++ 指向特定类型(C+;+;)的STL容器样式和迭代器_C++_Stl_Iterator - Fatal编程技术网

C++ 指向特定类型(C+;+;)的STL容器样式和迭代器

C++ 指向特定类型(C+;+;)的STL容器样式和迭代器,c++,stl,iterator,C++,Stl,Iterator,我有一个容器类(称为Atom),我想在其中存储Term类型的对象。许多STL容器构造函数都使用container(迭代器优先,迭代器最后)的形式,用一些元素集初始化容器 现在,我希望能够将此表单用于Atom类,但我不确定如何将迭代器从其容器类中解开。例如,目前我有: class Atom { public: Atom(std::string str, std::vector<Term>::const_iterator start, std::vector<

我有一个容器类(称为Atom),我想在其中存储Term类型的对象。许多STL容器构造函数都使用
container(迭代器优先,迭代器最后)
的形式,用一些元素集初始化容器

现在,我希望能够将此表单用于Atom类,但我不确定如何将迭代器从其容器类中解开。例如,目前我有:

class Atom {
public:
  Atom(std::string str, 
    std::vector<Term>::const_iterator start, 
    std::vector<Term>::const_iterator end);
类原子{
公众:
原子(std::string str,
std::vector::const_迭代器开始,
std::vector::const_迭代器端);

这只允许使用向量迭代器。如何概括我使用的迭代器类型?

每当需要概括类型时,请考虑模板:

class Atom {
public:
  template <typename ForwardIterator>
  Atom(std::string str, 
    ForwardIterator start, 
    ForwardIterator end);
类原子{
公众:
模板
原子(std::string str,
ForwardIterator开始,
转发器(迭代器端);

现在只需在范围内进行迭代,不管它是什么。

您可以将构造函数声明为自己的模板。只需将
开始
结束
视为迭代器,如果它们支持该接口,它们就会工作。如果复制构造函数无法转换,则不必担心强制迭代器处理类型
术语o一个
术语
就行了

构造函数可能如下所示:

template<typename I> Atom(std::string str, I start, I end);
模板原子(std::string str,I start,I end);