stl 迭代器的语法是如何实现的?

发布于 2024-09-03 10:51:20 字数 333 浏览 2 评论 0原文

我一直在业余时间编写一个库,以让自己更熟悉 C++ 和奇异值分解。我一直在致力于编写一个 Iterator 类,并且我完全有能力编写该功能,并且我已经为我自己的当前 MatrixIterator 类编写了功能。我猜测它涉及命名空间,因为:

vector<int>::iterator

似乎是命名空间向量的迭代器,但命名空间是我不熟悉的另一个主题。

我主要是问实现一个迭代器会涉及什么,以便可以以与 stl 迭代器类似的方式引用它。我也知道我可以使用 boost.iterators 或类似的东西来节省自己的大量工作,但我更感兴趣的是学习此类内容的所有细节。

I've been working on writing a library in my spare time to familiarize myself more with c++ and singular value decomposition. I've been working on writing an Iterator class and I'm entirely capable of writing the functionality and I have already for my own currently MatrixIterator class. I'm guessing that it involves namespaces because:

vector<int>::iterator

Would appear to be an iterator from the namespace vector, but namespaces are another topic which I'm not familiar with.

Mainly I'm asking what would it involve to implement an iterator such that it could be referenced in a similar way to the stl iterators. I'm also aware that I could use boost.iterators or something similar to save myself a lot of work, but I'm more interested in learning all of the details that go into something like this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

煮酒 2024-09-10 10:51:21

通常它是容器内的嵌套类或通过 :: 访问的 typedef。

网络上有大量带有迭代器的类示例(我不建议研究 STL 实现,因为如果您第一次查看它,很难理解)。

您可以查看 STX Btree 实现来查看精心设计的迭代器示例。< /em>

Usually it's a nested class inside your container or a typedef that is accessed via ::.

There is a huge amount of samples of classes with iterators on the web (I wouldn't recommend investigating into the STL implementation, because it's hard to understand if you're looking to it at the first time).

You could look at the STX Btree implementation to see a well-designed iterator sample.

捂风挽笑 2024-09-10 10:51:21

vector 是一个 class 而不是 namespace (重要区别),其定义如下:

template<typename T>
class vector{
    public:
        typedef some_type iterator;
    ...
};

where some_type< /code> 也很可能是向量的友元,甚至是成员类

vector<int> is a class not a namespace (important distinction), the definition is along the lines of:

template<typename T>
class vector{
    public:
        typedef some_type iterator;
    ...
};

where some_type will also most likely be a friend of vector, or even a member-class

壹場煙雨 2024-09-10 10:51:21

:: 运算符不仅可以访问名称空间,还可以访问名称空间。它还访问类的类型定义、静态成员、枚举等。

#include <iostream>

template <typename T>
class Foo
{
    public:
        static void write(T t)
        {
            std::cout << t << std::endl;
        }

        typedef T thing;
};

int main()
{
    Foo<int>::thing my_thing = 42;
    Foo<int>::write(my_thing);
}

The :: operator doesn't only access namespaces; it also accesses typedefs, static members, enums, etc. of a class.

#include <iostream>

template <typename T>
class Foo
{
    public:
        static void write(T t)
        {
            std::cout << t << std::endl;
        }

        typedef T thing;
};

int main()
{
    Foo<int>::thing my_thing = 42;
    Foo<int>::write(my_thing);
}
末蓝 2024-09-10 10:51:20

不,这与命名空间无关。它只是类中的 typedef:

template <typename T>
class container
{
public:
    typedef ... iterator;
};

一旦有了迭代器类,就需要实现几个运算符。对于前向迭代器,这将是:

operator*();
operator++();
operator==(const TYPE &other);

如果您想要一个可以完全参与 STL 其余部分的迭代器,那么您还需要做其他事情,例如为其指定一个类别。

No, this has nothing to do with namespaces. It's simply a typedef within a class:

template <typename T>
class container
{
public:
    typedef ... iterator;
};

Once you have a your iterator class, there are a couple of operators you need to implement. For a forward iterator, that would be:

operator*();
operator++();
operator==(const TYPE &other);

If you want an iterator that can fully participate with the rest of the STL, there are other things you need to do such as give it a category.

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