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

資訊專(zhuān)欄INFORMATION COLUMN

[TODO]Iterator, foreach, generics and callback in

CoXie / 1707人閱讀

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)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/37620.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...

    cheukyin 評(píng)論0 收藏0
  • forEach到迭代器

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

    rockswang 評(píng)論0 收藏0
  • [Leetcode] Peeking Iterator 瞥一眼迭代器

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

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

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

    tinyq 評(píng)論0 收藏0
  • Underscore和Lo-Dash中的Collections _.each

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

    weakish 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

CoXie

|高級(jí)講師

TA的文章

閱讀更多
最新活動(dòng)
閱讀需要支付1元查看
<