[英]virtual forward iterator?
I looked at STL's forward iterator.我查看了 STL 的前向迭代器。 I don't see any virtual functions anywhere.
我在任何地方都看不到任何虚拟功能。 If I have a library that wants a forward iterator of strings how can I allow the function to take any forward iterator that returns strings?
如果我有一个需要字符串前向迭代器的库,我如何允许 function 采用任何返回字符串的前向迭代器? Is there anything in the standard library I can use?
标准库中有什么我可以使用的吗?
There are no virtual methods as they are not meant to be used polymorphically (in the common sense of runtime polymorphism) but rather through templates (static polymorphism).没有虚拟方法,因为它们不是用于多态(在运行时多态的常识中),而是通过模板(静态多态)使用。 The common approach is having the function that takes the iterators templatized on the type of the iterator.
常见的方法是使用 function 将迭代器模板化为迭代器的类型。 You can find many examples in the STL algorithm header:
您可以在 STL 算法 header 中找到许多示例:
template <class InputIterator, class Distance>
inline void distance(InputIterator first, InputIterator last, Distance& n);
In your particular case:在您的特定情况下:
template <class ForwardIterator> // or InputIterator or whatever your needs are
void acceptIterators( ForwardIterator start, ForwardIterator end );
The additional restriction that it has to refer to strings can be implemented in term of type traits and enable_if:它必须引用字符串的附加限制可以根据类型特征和 enable_if 来实现:
template <class ForwardIterator>
std::enable_if< std::is_same< std::string,
typename iterator_traits<ForwardIterator>::value_type > >
void acceptIterators( ForwardIterator start, ForwardIterator end );
Using typetraits from c++0x, but the enable_if
and is_same
are available in boost if you need them in a compiler that does not have support for them.使用来自 c++0x 的 typetraits,但如果您在不支持它们的编译器中需要它们,则可以在 boost 中使用
enable_if
和is_same
。
If you need to be able to switch the types of iterators at runtime, you might want to look into any_iterator
that performs type erasure on the specific iterators providing a runtime polymorphic interface.如果您需要能够在运行时切换迭代器的类型,您可能需要查看
any_iterator
,它在提供运行时多态接口的特定迭代器上执行类型擦除。 It is described in the article On the Tension Between Object-Oriented and Generic Programming in C++ , the implementation can be found in Adobe STLab .在 C++ 中的On the Tension between Object-Oriented and Generic Programming 一文中有描述,实现可以在Adobe STLab中找到。
You can write your own iterator and use it as:您可以编写自己的迭代器并将其用作:
std::for_each( make_myiterator(c.begin), make_myiterator(c.end), do_some() );
You can inherit from std::iterator
, or check Boost Iterator Facade — it will help you to write own iterator.你可以从
std::iterator
继承,或者查看Boost Iterator Facade——它将帮助你编写自己的迭代器。
The usual way is to make functions that use iterator function-templates.通常的方法是制作使用迭代器函数模板的函数。 It's what the standard library does.
这就是标准库所做的。
If you want to accept any iterator over eg strings you need to use templates.如果您想接受任何迭代器,例如字符串,您需要使用模板。 There's no top-level iterator class that all other iterators inherit.
没有所有其他迭代器继承的顶级迭代器 class 。
Writing conformant C++ iterators can be a pain, but Boost.Iterators helps you a great deal if your not afraid of templates.编写符合标准的 C++ 迭代器可能会很痛苦,但如果您不害怕模板, Boost.Iterators会为您提供很大帮助。 There's an example on how to use iterator_facade to create a simple iterator that you should have a look at.
有一个关于如何使用 iterator_facade 创建简单迭代器的示例,您应该看看。 Boost.Iterators is a header only library so there's no need to link anything with your application/library.
Boost.Iterators 是一个仅限 header 的库,因此无需将任何内容与您的应用程序/库链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.