了解STL中的迭代器 [英] Understanding Iterators in the STL

查看:137
本文介绍了了解STL中的迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有点困惑迭代器和他们实际是....在C ++ STL

Hi so im a little confused about iterators and what they actually are.... in the C++ STL

在这种情况下im使用列表,不明白为什么必须创建迭代器:

std :: list< int> :: const_iterator iElementLocator;

In this case im using a list, and i don't understand why you have to make an iterator:
std::list <int>::const_iterator iElementLocator;

通过derefrence运算符将列表中的内容覆盖:

cout< * iElementLocator;

to dipslay contents of the list by the derefrence operator:
cout << *iElementLocator;

之后

>请解释一个迭代器究竟是什么,为什么我要derefrence它/使用它!

谢谢!!

Please explain what exactly an iterator is and why i have to derefrence it /use it!
Thanks!!

推荐答案

STL中有三个构建块:

There are three building blocks in the STL:


  • 容器

  • 算法

  • 迭代器

在概念级别,容器保存数据。这本身并不是非常有用,因为你想对数据某事;您想要操作,操作它,查询它,玩它。算法做到这一点。但算法不保留数据,没有数据 - 他们需要一个容器来执行此任务。给一个算法的容器,并且你有一个动作。

At the conceptual level containers hold data. That by itself isn't very useful, because you want to do something with the data; you want to operate on it, manipulate it, query it, play with it. Algorithms do exactly that. But algorithms don't hold data, they have no data -- they need a container for this task. Give a container to an algorithm and you have an action going on.

剩下的唯一的问题是算法如何穿越容器,从技术角度。技术上,容器可以是链表,或者它可以是数组,二叉树或可以保存数据的任何其他数据结构。但是遍历数组的方式不同于遍历二叉树。虽然概念上所有算法都希望从容器中一次获取一个元素,然后对该元素进行处理,但从容器中获取下一个元素的操作在技术上非常容器化,

The only problem left to solve is how does an algorithm traverse a container, from a technical point of view. Technically a container can be a linked list, or it can be an array, or a binary tree, or any other data structure that can hold data. But traversing an array is done differently than traversing a binary tree. Even though conceptually all an algorithm wants is to "get" one element at a time from a container, and then work on that element, the operation of getting the next element from a container is technically very container-specific.

它似乎需要为每个容器编写相同的算法,因此每个版本的算法都有正确的代码,用于遍历容器。但是有一个更好的解决方案:要求容器返回一个可以遍历容器的对象。对象将有一个接口算法知道。当算法要求对象获得下一个元素时,对象将遵从。因为对象直接来自容器,它知道如何访问容器的数据。因为对象有一个接口算法知道,我们不需要为每个容器复制一个算法。

It appears as if you'd need to write the same algorithm for each container, so that each version of the algorithm has the correct code for traversing the container. But there's a better solution: ask the container to return an object that can traverse over the container. The object would have an interface algorithms know. When an algorithm asks the object to "get the next element" the object would comply. Because the object came directly from the container it knows how to access the container's data. And because the object has an interface the algorithm knows, we need not duplicate an algorithm for each container.

这是迭代器。

这里的迭代器粘贴到容器中,而不需要耦合两个。迭代器耦合到容器,并且算法耦合到迭代器的接口。这里的魔法源是真正的模板编程。考虑标准的 copy()算法:

The iterator here glues the algorithm to the container, without coupling the two. An iterator is coupled to a container, and an algorithm is coupled to the iterator's interface. The source of the magic here is really template programming. Consider the standard copy() algorithm:

template<class In, class Out>
Out copy(In first, In last, Out res)
{
    while( first != last ) {
        *res = *first;
        ++first;
        ++res;
    }
    return res;
}

>算法以类型 In 和类型 Out 的一个迭代器为模板的两个迭代器作为参数。它将从开始开始并在最后之前结束的元素复制到 res 。该算法知道要获得下一个元素,它需要说 ++第一 ++ res 。它知道读取一个元素,它需要说 x = * first 并写一个元素,它需要说 * res = x 。这是接口算法假设和迭代器承诺的一部分。如果错误地迭代器不符合接口,那么编译器将发出一个错误,通过类型 In Out ,当类型没有定义函数时。

The copy() algorithm takes as parameters two iterators templated on the type In and one iterator of type Out. It copies the elements starting at position first and ending just before position last, into res. The algorithm knows that to get the next element it needs to say ++first or ++res. It knows that to read an element it needs to say x = *first and to write an element it needs to say *res = x. That's part of the interface algorithms assume and iterators commit to. If by mistake an iterator doesn't comply with the interface then the compiler would emit an error for calling a function over type In or Out, when the type doesn't define the function.

这篇关于了解STL中的迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆