有没有 C++相当于Java的STL容器类的Collection接口吗?
我想传递任意容器作为函数的参数并对其进行迭代(不擦除或推送元素)。不幸的是,似乎没有标准的方法可以做到这一点。
我想到的第一个解决方案是一个由包装 STL 容器的类实现的接口(我们称之为 CollectionInterface
)。所以函数声明看起来像:
f(const CollectionInterface * collection);
或者,我正在考虑方法模板,它的优点是在编译时保持绑定:
template <class CONTAINER> void f(const CONTAINER & collection);
您认为哪种方式更好?
I would like to pass arbitrary container as an argument of function and iterate over it (no erasing nor pushing elements). Unfortunately it looks like there is no standard way of doing this.
First solution which comes to my mind is an interface (let's call it CollectionInterface
) implemented by classes that will wrap STL containers. so the function declaration would look like:
f(const CollectionInterface * collection);
Or, I was thinking about method template, which has an advantage that it keeps binding at compilation time:
template <class CONTAINER> void f(const CONTAINER & collection);
Which way do you think is better?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ForwardIterator?这是一种 InputIterator(或 OutputIterator)类型,也允许多遍算法(增加它不会使先前的值无效)。
迭代器(与 Java 迭代器有很大不同)是统一 C++ 集合的中心线程。有关使用它们的算法示例(以及相关的迭代器类型要求),您可以从
< 开始;算法>
。特别是, search 提供了使用 ForwardIterator 的示例。它查找由[first2, last2)
范围定义的序列在[first1, last1]
范围内的第一个匹配项。这些都是满足ForwardIterator要求的对象。ForwardIterator? This is a type of InputIterator (or OutputIterator) that also allows multi-pass algorithms (incrementing it does not invalidate prior values).
Iterators (which are quite different from Java iterators) are the central thread unifying C++ collections. For examples of algorithms working on them (and associated iterator type requirements), you can start with
<algorithm>
. In particular, search provides an example of using ForwardIterator. It finds the first occurrence within the range[first1, last1]
of the sequence defined by the range[first2, last2)
. These are all objects meeting the requirements ofForwardIterator
.如果您希望以这种方式处理事情,您还可以编写接受整个容器而不是引用的方法。标准库容器的迭代器都是通过成员函数
begin()
和end()
提供的,或者在某些情况下通过rbegin()
和 < code>rend() 用于向后迭代。模板的工作方式是,您不必创建派生对象的实际接口类型;相反,需求是通过使用的对象来推断的。传递迭代器在使用函数时提供了更大的灵活性,特别是并非所有迭代器都来自具有显式
begin()
和end()
函数的容器,并且您可以提供任何显式你想要的子范围。但有时这种方法是合适的。You can also write methods that accept the entire container instead of a reference if that's the way you want to handle things. Iterators into standard library containers are all provided via the member functions
begin()
andend()
, or in some casesrbegin()
andrend()
for iterating backwards. The way templates work, you don't have to create an actual interface type that objects derive from; the requirements are instead inferred by the object is used.Passing iterators provide more flexibility when using the functions, particularly in that not all iterators come from containers with explicit
begin()
andend()
functions, and you can provide whatever explicit subrange you want. But sometimes this method is appropriate.传递迭代器。下面是一个实现和使用的示例:
请注意,在许多情况下,您实际上不需要编写该函数,但您可以使用库工具来编写它,然后只需应用
std::for_each
关于这一点。或者更好的是,使用预先存在的算法,例如 std::accumulate 或 std::find_if 。Pass iterators. Here is an example for implementation and use:
Note that in many cases, you don't actually need to write the function, but you can compose it using the library facilities instead and then simply apply
std::for_each
on that. Or even better, use a preexisting algorithm likestd::accumulate
orstd::find_if
.