成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[TODO]Iterator, foreach, generics and callback in

cheukyin / 951人閱讀

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

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/64605.html

相關(guān)文章

  • [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...

    CoXie 評論0 收藏0
  • forEach到迭代器

    摘要:本文從使用對數(shù)組進行遍歷開始說起,粗略對比使用進行遍歷的差異,并由此引入中可迭代對象迭代器的概念,并對其進行粗略介紹。說到這里,就繼續(xù)說一下迭代器關(guān)閉的情況了。確實,符合可迭代協(xié)議和迭代器協(xié)議的。 本文從使用 forEach 對數(shù)組進行遍歷開始說起,粗略對比使用 forEach , for...in , for...of 進行遍歷的差異,并由此引入 ES6 中 可迭代對象/迭代器 的概...

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

    摘要:當?shù)臅r候除了要返回緩存,還要將緩存更新為下一個數(shù)字,如果沒有下一個就將緩存更新為。代碼后續(xù)如何支持任意類型的迭代器使用的方式,代碼中已經(jīng)將替換為的 Peeking Iterator Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIt...

    smallStone 評論0 收藏0
  • (Thinking in Java)第15章 泛型

    摘要:反省發(fā)放需要將泛型參數(shù)列表之余返回值之前杠桿利用類型參數(shù)推斷現(xiàn)在可以了,別?,F(xiàn)在可以顯式的類型說明這段代碼沒毛病的,可變參數(shù)與泛型方法沒啥好說用于的泛型方法方法可以透明的應(yīng)用于實現(xiàn)了泛型接口的類。但是這個卻可以指向各種是的對象。 二、簡單泛型 2.一個堆棧類 package tij.generic; public class Test { public static void...

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

    摘要:遍歷集合,對集合中的每個元素執(zhí)行回調(diào)。因此,上面的判斷等價于是預(yù)先定義的空對象,內(nèi)部用于提前結(jié)束循環(huán)的標志,并沒有對外公開。 _.each 遍歷集合,對集合中的每個元素執(zhí)行回調(diào)。 API Lo-Dash _.forEach(collection [, callback=identity, thisArg]) Aliases each Arguments collection (Arr...

    weakish 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<