簡體   English   中英

如何檢測地圖迭代器中的最后一個元素

[英]How to detect last element within a map iterator

我希望能夠檢測我的map迭代器中的最后一次迭代。 我怎么做到這一點?

class JSON {
public:
    static void stringify(map<string, string> data)
    {
        string base;

        base += "{ ";

        for (map<string, string>::iterator it = data.begin(); it != data.end(); ++it)
        {
            cout << it->first.c_str() << " => " << it->second.c_str() << endl;
        }
    }
};

您可以像這樣使用std :: prev

    for(auto it = data.begin(); it != data.end(); ++it)
    {
        if(it == std::prev(data.end()))
        {
            // this is the last iteration
        }

        std::cout << it->first << " => " << it->second << '\n';
    }

std :: prev從其參數返回前一個迭代器。

在每個循環迭代中,您可以檢查下一個迭代器是否與map的end()迭代器匹配,例如:

static void stringify(map<string, string> data)
{
    string base;

    base += "{ ";

    auto it = data.begin();
    auto end = data.end();

    while (it != end)
    {
        auto next_it = std::next(it);

        if (next_it == end) {
            cout << "this is the last iteration!" << endl;
        }

        cout << it->first << " => " << it->second << endl;

        it = next_it;
    }
}

要么:

static void stringify(map<string, string> data)
{
    string base;

    base += "{ ";

    auto it = data.begin();
    auto end = data.end();

    if (it != end)
    {
        do
        {
            cout << it->first << " => " << it->second << endl;

            auto next_it = std::next(it);
            if (next_it == end) {
                cout << "that was the last iteration!" << endl;
                break;
            }

            it = next_it;
        }
        while (true);
    }
}

如果您的目標只是避免在第一次或最后一次迭代時將逗號插入JSON輸出中(取決於您希望在插入代碼中的位置),您可以這樣做:

static void stringify(map<string, string> data)
{
    string base = "{";

    auto it = data.begin();
    auto end = data.end();

    if (it != end)
    {
        cout << it->first << " => " << it->second << endl;

        base += (" \"" + it->first + "\": \"" + it->second + "\"");

        while (++it != end)
        {
            cout << it->first << " => " << it->second << endl;

            base += (", \"" + it->first + "\": \"" + it->second + "\"");
        }
    }

    base += " }";
}

這可以通過創建索引,在地圖的每次迭代中遞增索引,然后在每次迭代中將地圖的大小與索引進行比較來實現。

class JSON {
public:
    static void stringify(map<string, string> data)
    {
        string base;
        int index = 0;

        base += "{ ";

        for (map<string, string>::iterator it = data.begin(); it != data.end(); ++it)
        {
            if (data.size() - 1 == index)
            {
                // Do stuff here
            }

            cout << it->first.c_str() << " => " << it->second.c_str() << endl;
            index++;
        }
    }
};

您可以簡單地將迭代器邊界檢查移動到循環體中:

void stringify(map<string, string> data){
    string base;

    base += "{ ";

    for (map<string, string>::iterator it = data.begin();;){
        if (it == data.end()){
            cout << "Last iteration!";
            break;
        }
        cout << it->first << " => " << it->second << endl;
        ++it;
    }
}

請注意,將為空映射調用if語句中的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM