map删除元素方法

网上搜索了竟然还有错的,真是蛋疼。

#include <iostream>
#include <map>
using namespace std;
#ifndef foreach
#define foreach(container,it) \
    for(typeof(container.begin()) it = container.begin();it != container.end();++it)
#endif
int main()
{
    map<int,int> mtest;
    for(int i = 0;i < 10;++i){
        mtest[i] = i*i;
    }
    for(map<int,int>::iterator iter = mtest.begin();iter != mtest.end();)
    {
        if(iter->second == 25 || iter->second == 36){
            mtest.erase(iter++);
        }
        else{
            ++iter;
        }
    }
    //注:iter = mtest.earse(iter);这种在Linux上编译不过
    foreach(mtest,it){
        cout <<it->first << ":" <<it->second << " ";
    }
    cout <<endl;
}

猜你喜欢

转载自blog.csdn.net/wWX336815/article/details/81563623
今日推荐