STL:map的使用:删除map中指定值为value的所有元素

// 第一个解决方案
//erase.h
#include <algorithm>
using std::find_if; 
 
template <class _map>
class Proxy{
    typename _map::mapped_type   const _value;
public :
    Proxy(typename _map::mapped_type const &value):_value(value){}
    bool operator()(typename _map::value_type & value){
       return value.second==_value;
    }
};
 
 
template <class _map>
size_t Erase(_map& m,typename _map::mapped_type   const &value)
{  
    size_t count=0;
    typename _map::iterator iter;
    typename _map::iterator begin=m.begin();
    typename _map::iterator end=m.end();
    Proxy<_map> proxy(value);
    while((iter=find_if(begin,end,proxy))!=end)
    {
       ++count;
       m.erase(iter);
       begin=m.begin();
       end=m.end();
    }  
    return count;
}
 
 
// 示例代码
#include <string>
#include <map>
#include "erase.h"
using std::map;
using std::string;
typedef map<int ,string> Int2Str;
int main(){  
    Int2Str sample;  
    sample[1]=string("abc");
    sample[2]=string("abc");
    sample[3]=string("1234");
    sample[4]=string("abc");
    Erase(sample,string("abc"));   
    return 0;
}
//******************************************************************//
 
// 第二种方法
#include <map>
#include <string>
#include <algorithm>
#include <iostream>
using std::map;
using std::string;
using std::find_if;
using std::endl;
using std::cout;
 
template <class _MAP>
class DeleteAllValueProxy{
    typedef typename _MAP::mapped_type const map_type;
    typedef typename _MAP::iterator map_iter;
    typedef typename _MAP::const_reference map_value;
 
    map_type _val;
public :
    DeleteAllValueProxy(_MAP &rmap,map_type &val)
       :_val(val)
    {
       map_iter begin=rmap.begin();
       map_iter end=rmap.end();
       map_iter iter=find_if(begin,end,*this);
       while (iter!=end)
       {
           cout<<iter->first<<"   "<<iter->second<<endl;
           begin=rmap.erase(iter);
           end=rmap.end();
           iter=find_if(begin,end,*this);
       }
    }
    bool operator()(map_value item){
       return item.second==_val;
    }
};
template <class _MAP>
inline void DeleteAllValue(_MAP&rmap,typename _MAP::mapped_type const&val)
{
    (DeleteAllValueProxy<_MAP>(rmap,val));
}
 
int main()
{
    map<int,string> Int2Str;
    Int2Str[5]="abc";
    Int2Str[12]="abc";
    Int2Str[43]="abc";
    Int2Str[27]="abc";
    Int2Str[1]="def";
    Int2Str[8]="ghi";
    Int2Str[24]="jkl";
    Int2Str[56]="mn";
    Int2Str[0]="opq";
    DeleteAllValue(Int2Str,string("abc"));
    return 0;
}
 
//*******************************************************************/
// 第三种方法
 
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
using std::endl;
using std::cout;
using std::find_if;
using std::string;
using std::map;
template <class _map>
void Erase(_map& m,typename _map::mapped_type   const &value)
{
    typename _map::iterator iter=m.begin();
    typename _map::iterator end=m.end();
    typename _map::iterator temp;
    while (iter!=end)
    {
       if (iter->second==value)
       {
           temp=iter;
           ++iter;
           cout<<temp->first<<" "<<temp->second<<endl;
           m.erase(temp);
          
       }
       else
           ++iter;
       end=m.end();
    }
}
typedef map<int ,string> Int2Str;
 
int main(){  
    Int2Str sample;  
    sample[11]=string("abc");
    sample[6]=string("bcd");
    sample[3]=string("afd");
    sample[4]=string("abc");
    sample[15]=string("gif");
    sample[44]=string("abc");
    sample[12]=string("gif");
    sample[47]=string("abc");
    sample[356]=string("364");
    sample[42]=string("abc");
 
    Erase(sample,string("abc"));   
    return 0;
}
 

 转自http://blog.csdn.net/SammyLan/archive/2006/09/10/1203753.aspx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值