久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

remove_if 等效于 std::map

remove_if equivalent for std::map(remove_if 等效于 std::map)
本文介紹了remove_if 等效于 std::map的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我試圖根據(jù)特定條件從地圖中刪除一系列元素.我如何使用 STL 算法做到這一點(diǎn)?

I was trying to erase a range of elements from map based on particular condition. How do I do it using STL algorithms?

最初我想使用 remove_if 但這是不可能的,因?yàn)?remove_if 不適用于關(guān)聯(lián)容器.

Initially I thought of using remove_if but it is not possible as remove_if does not work for associative container.

是否有任何適用于地圖的remove_if"等效算法?

Is there any "remove_if" equivalent algorithm which works for map ?

作為一個(gè)簡單的選擇,我想到了遍歷地圖并擦除.但是循環(huán)遍歷地圖并擦除安全選項(xiàng)嗎?(因?yàn)椴脸蟮鳠o效)

As a simple option, I thought of looping through the map and erase. But is looping through the map and erasing a safe option?(as iterators get invalid after erase)

我使用了以下示例:

bool predicate(const std::pair<int,std::string>& x)
{
    return x.first > 2;
}

int main(void) 
{

    std::map<int, std::string> aMap;

    aMap[2] = "two";
    aMap[3] = "three";
    aMap[4] = "four";
    aMap[5] = "five";
    aMap[6] = "six";

//      does not work, an error
//  std::remove_if(aMap.begin(), aMap.end(), predicate);

    std::map<int, std::string>::iterator iter = aMap.begin();
    std::map<int, std::string>::iterator endIter = aMap.end();

    for(; iter != endIter; ++iter)
    {
            if(Some Condition)
            {
                            // is it safe ?
                aMap.erase(iter++);
            }
    }

    return 0;
}

推薦答案

差不多.

for(; iter != endIter; ) {
     if (Some Condition) {
          iter = aMap.erase(iter);
     } else {
          ++iter;
     }
}

如果您確實(shí)從中刪除了一個(gè)元素,那么您最初擁有的會(huì)增加迭代器兩次;您可能會(huì)跳過需要?jiǎng)h除的元素.

What you had originally would increment the iterator twice if you did erase an element from it; you could potentially skip over elements that needed to be erased.

這是我在很多地方看到使用和記錄的常用算法.

This is a common algorithm I've seen used and documented in many places.

您是正確的,迭代器在擦除后失效,但只有引用被擦除元素的迭代器,其他迭代器仍然有效.因此在 erase() 調(diào)用中使用 iter++.

You are correct that iterators are invalidated after an erase, but only iterators referencing the element that is erased, other iterators are still valid. Hence using iter++ in the erase() call.

這篇關(guān)于remove_if 等效于 std::map的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Assertion failed (size.widthgt;0 amp;amp; size.heightgt;0)(斷言失敗(size.width0 amp;amp; size.height0))
Rotate an image in C++ without using OpenCV functions(在 C++ 中旋轉(zhuǎn)圖像而不使用 OpenCV 函數(shù))
OpenCV: process every frame(OpenCV:處理每一幀)
Why can#39;t I open avi video in openCV?(為什么我不能在 openCV 中打開 avi 視頻?)
OpenCV unable to set up SVM Parameters(OpenCV 無法設(shè)置 SVM 參數(shù))
Convert a single color with cvtColor(使用 cvtColor 轉(zhuǎn)換單一顏色)
主站蜘蛛池模板: 国产精品久久av | 综合精品 | 欧美自拍另类 | 91精品国产自产精品男人的天堂 | 五月婷婷 六月丁香 | 日韩影院一区 | 狠狠热视频 | 嫩草最新网址 | 国产中文字幕在线观看 | 国产亚洲精品a | 日韩欧美天堂 | 日韩视频在线播放 | 欧美老妇交乱视频 | 亚洲国产成人在线视频 | 亚洲欧洲精品在线 | 九色av | 亚洲国产一区二区三区 | 荷兰欧美一级毛片 | 亚洲精品久久久久国产 | 国产精品一区二区在线 | 中文字幕国产精品 | 久久国内精品 | 国产在线二区 | 少妇性l交大片免费一 | 精品国产乱码久久久久久蜜柚 | 成人在线视频免费看 | 久久精品亚洲 | 久久精品国产99国产精品 | 欧美a级成人淫片免费看 | 亚洲一区不卡 | 一区二区在线不卡 | 亚洲 成人 在线 | 91亚洲精品在线观看 | 久久久69 | 日韩在线欧美 | 久久88| 中文字幕日本一区二区 | 一区二区视频在线 | 午夜av免费 | 午夜影院在线观看免费 | 青草青草久热精品视频在线观看 |