简体   繁体   English

Stl迭代器的语法如何实现?

[英]How is the syntax for stl iterators implemented?

I've been working on writing a library in my spare time to familiarize myself more with c++ and singular value decomposition. 我一直在业余时间编写库,以使自己更加熟悉c ++和奇异值分解。 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. 我一直在编写Iterator类,并且完全有能力编写该功能,并且我已经拥有了自己的当前MatrixIterator类。 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. 我主要是在问实现一个迭代器会涉及到什么,以便可以与stl迭代器类似的方式对其进行引用。 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. 我也知道我可以使用boost.iterators或类似的东西来节省很多工作,但是我对学习像这样的所有细节更感兴趣。

No, this has nothing to do with namespaces. 不,这与名称空间无关。 It's simply a typedef within a class: 它只是类中的typedef:

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. 如果您希望迭代器可以完全参与STL的其余部分,则还需要执行其他操作,例如为其指定类别。

Usually it's a nested class inside your container or a typedef that is accessed via :: . 通常,它是容器中的嵌套类或通过::访问的typedef。

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). Web上有大量带有迭代器的类示例(我不建议您研究STL实现,因为很难理解是否是第一次使用它。)

You could look at the STX Btree implementation to see a well-designed iterator sample. 您可以查看STX Btree实现,以查看设计良好的迭代器示例。

vector<int> is a class not a namespace (important distinction), the definition is along the lines of: vector<int>是一个不是namespaceclass (重要区别),其定义大致如下:

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 其中some_type也很可能是vector的friend ,甚至是成员类

The :: operator doesn't only access namespaces; ::运算符不仅访问名称空间; it also accesses typedefs, static members, enums, etc. of a class. 它还访问类的typedef,静态成员,枚举等。

#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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM