资讯专栏INFORMATION COLUMN

[TODO]Iterator, foreach, generics and callback in

CoXie / 1681人阅读

It seems like a huge topic, so just divide it into small pieces and conquer them piece by piece, I have drawn a map for this article to guide me finish it instead of being scared by it.

Iterator

Concept of iterator is here in #Wikipedia: Iterator , so I think iterator is just like pointer in C or cursor in database.

why use iterator

Iterator is just an abstract concept and can be implemented in many different ways. But first of all, we should figure out why we need iterator and why it is important for us.

First, just think about a simple for loop in C++:

#include 
using namespace std;

int main()
{
    for (int i = 0; i < 3; i++)
        cout << i << " ";
    cout << endl;

    system("pause");
    return 0;
}
#include 
using namespace std;

int main()
{
    int a[] = { 1, 2, 3 };
    for (int i = 0; i < 3; i++)
        cout << a[i] << " ";
    cout << endl;

    system("pause");
    return 0;
}
#include 
using namespace std;

int main()
{
    int a[] = { 1, 2, 3 };
    for (int i = 0; i < 3; i++)
        cout << *(a+i) << " ";
    cout << endl;

    system("pause");
    return 0;
}
#include 
#include 
using namespace std;

int main()
{
    vector a;
    a.push_back(1);
    a.push_back(2);
    a.push_back(3);

    for (vector::iterator i = a.begin(); 
                               i != a.end();
                               i++)
        cout << *i << " ";
    cout << endl;

    system("pause");
    return 0;
}
#include 
#include 
using namespace std;

int main()
{
    vector a;
    a.push_back(1);
    a.push_back(2);
    a.push_back(3);

    for (auto i : a)
        cout << i << " ";
    cout << endl;

    system("pause");
    return 0;
}

In C++, if we implement a container of our own, and we want to use for loop or while loop to traverse it like basic data type, we can use iterator by implement

So first let"s analysis this example in

C++ version Python version C# version

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/37620.html

相关文章

  • [TODO]Iterator, foreach, generics and callback in

    It seems like a huge topic, so just divide it into small pieces and conquer them piece by piece, I have drawn a map for this article to guide me finish it instead of being scared by it. Iterator Conce...

    cheukyin 评论0 收藏0
  • forEach到迭代器

    摘要:本文从使用对数组进行遍历开始说起,粗略对比使用进行遍历的差异,并由此引入中可迭代对象迭代器的概念,并对其进行粗略介绍。说到这里,就继续说一下迭代器关闭的情况了。确实,符合可迭代协议和迭代器协议的。 本文从使用 forEach 对数组进行遍历开始说起,粗略对比使用 forEach , for...in , for...of 进行遍历的差异,并由此引入 ES6 中 可迭代对象/迭代器 的概...

    rockswang 评论0 收藏0
  • [Leetcode] Peeking Iterator 瞥一眼迭代器

    摘要:当的时候除了要返回缓存,还要将缓存更新为下一个数字,如果没有下一个就将缓存更新为。代码后续如何支持任意类型的迭代器使用的方式,代码中已经将替换为的 Peeking Iterator Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIt...

    smallStone 评论0 收藏0
  • (Thinking in Java)第15章 泛型

    摘要:反省发放需要将泛型参数列表之余返回值之前杠杆利用类型参数推断现在可以了,别。现在可以显式的类型说明这段代码没毛病的,可变参数与泛型方法没啥好说用于的泛型方法方法可以透明的应用于实现了泛型接口的类。但是这个却可以指向各种是的对象。 二、简单泛型 2.一个堆栈类 package tij.generic; public class Test { public static void...

    tinyq 评论0 收藏0
  • Underscore和Lo-Dash中的Collections _.each

    摘要:遍历集合,对集合中的每个元素执行回调。因此,上面的判断等价于是预先定义的空对象,内部用于提前结束循环的标志,并没有对外公开。 _.each 遍历集合,对集合中的每个元素执行回调。 API Lo-Dash _.forEach(collection [, callback=identity, thisArg]) Aliases each Arguments collection (Arr...

    weakish 评论0 收藏0

发表评论

0条评论

CoXie

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<