有没有 C++相当于Java的STL容器类的Collection接口吗?

发布于 2024-09-06 16:12:21 字数 379 浏览 3 评论 0原文

我想传递任意容器作为函数的参数并对其进行迭代(不擦除或推送元素)。不幸的是,似乎没有标准的方法可以做到这一点。

我想到的第一个解决方案是一个由包装 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

往昔成烟 2024-09-13 16:12:21

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 of ForwardIterator.

め可乐爱微笑 2024-09-13 16:12:21

如果您希望以这种方式处理事情,您还可以编写接受整个容器而不是引用的方法。标准库容器的迭代器都是通过成员函数 begin()end() 提供的,或者在某些情况下通过 rbegin() 和 < code>rend() 用于向后迭代。模板的工作方式是,您不必创建派生对象的实际接口类型;相反,需求是通过使用的对象来推断的。

template<typename Container> void Function(const Container& c) {
    for(typename Container::const_iterator i = c.begin(), end = c.end(); i != end; ++i)
       //do something
}

传递迭代器在使用函数时提供了更大的灵活性,特别是并非所有迭代器都来自具有显式 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() and end(), or in some cases rbegin() and rend() 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.

template<typename Container> void Function(const Container& c) {
    for(typename Container::const_iterator i = c.begin(), end = c.end(); i != end; ++i)
       //do something
}

Passing iterators provide more flexibility when using the functions, particularly in that not all iterators come from containers with explicit begin() and end() functions, and you can provide whatever explicit subrange you want. But sometimes this method is appropriate.

云巢 2024-09-13 16:12:21

我想传递任意容器作为函数的参数并对其进行迭代(不擦除或推送元素)。

传递迭代器。下面是一个实现和使用的示例:

template <typename Iter>
void function(Iter begin, Iter end)
{
    for (Iter it = begin; it != end; ++it)
    {
        std::cout << *it << std::endl;
    }
}

int main()
{
    std::string array[] = {"hello", "array", "world"};
    function(array, array + 3);

    std::vector<std::string> vec = {"hello", "vector", "world"};
    function(vec.begin(), vec.end());
}

请注意,在许多情况下,您实际上不需要编写该函数,但您可以使用库工具来编写它,然后只需应用 std::for_each 关于这一点。或者更好的是,使用预先存在的算法,例如 std::accumulate 或 std::find_if 。

I would like to pass arbitrary container as an argument of function and iterate over it (no erasing nor pushing elements).

Pass iterators. Here is an example for implementation and use:

template <typename Iter>
void function(Iter begin, Iter end)
{
    for (Iter it = begin; it != end; ++it)
    {
        std::cout << *it << std::endl;
    }
}

int main()
{
    std::string array[] = {"hello", "array", "world"};
    function(array, array + 3);

    std::vector<std::string> vec = {"hello", "vector", "world"};
    function(vec.begin(), vec.end());
}

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 like std::accumulate or std::find_if.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文