Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > 在for循环里对std::map进行元素移除
【标  题】:在for循环里对std::map进行元素移除
【关键字】:for,std,map
【来  源】:http://www.cppblog.com/lai3d/archive/2007/04/05/21274.html

在for循环里对std::map进行元素移除

Your Ad Here 在for循环里对std::map进行元素移除 - 阿来有话说 - C++博客
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  48 随笔 :: 0 文章 :: 59 评论 :: 0 Trackbacks

不多说了,看代码
#include <iostream>
#include 
<string>
#include 
<map>
#include 
<algorithm>

template
<class _Ty>
struct stPrintElement
    : 
public std::unary_function<_Ty, void>
{
    
void operator()( const _Ty& Arg )
    {
        std::cout 
<< Arg.second << std::endl;
    }
};


int _tmain(int argc, _TCHAR* argv[])
{
    typedef std::map
<int, std::string> tMap;
    typedef tMap::iterator tMapIterator;

    tMap MyMap;

    std::
string str = "I'm the first!";
    MyMap.insert(tMap::value_type(
0, str));

    str 
= "I'm the second!";
    MyMap.insert(tMap::value_type(
1, str));

    std::for_each(MyMap.begin(), MyMap.end(), stPrintElement
< std::pair<int, std::string> >());

    
for (tMapIterator it = MyMap.begin(); it != MyMap.end();)
    {
        
if (it->second == str)
        {
            MyMap.erase(it
++); /// Really smart! :-)
        }
        
else
        {
            
++it;
        }
    }

    std::cout 
<< "After erase: " << std::endl;
    std::for_each(MyMap.begin(), MyMap.end(), stPrintElement
< std::pair<int, std::string> >());

    
return 0;
}

后缀++解决了问题,哈哈
这个在《c++标准程序库》里有介绍,一直没有用到这个,今天用到了

2007/04/05 重构:
看了小明同志的回复后,优化下
#include <iostream>
#include 
<string>
#include 
<map>
#include 
<algorithm>
#include 
<vector>

template
<class TElement>
struct stPrintPairContainerElement
    : 
public std::unary_function<TElement, void>
{
    
void operator()( const TElement& elem )
    {
        std::cout 
<< elem.first
            
<< " : "
            
<< elem.second 
            
<< std::endl;
    }
};

template
<class TElement>
struct stPrintNoPairContainerElement
    : 
public std::unary_function<TElement, void>
{
    
void operator()( const TElement& elem ) const
    {
        std::cout 
<< elem << std::endl;
    }
};

template
<class TLeft, class TRight>
struct stPred
    : 
public std::binary_function<TLeft, TRight, bool>
{
    
bool operator()( const TLeft& left , const TRight& right) const /// 最后这个const不加不行
    {
        
return left.second == right;
    }
};

/// for vector, deque 
template <class TContainer, class TElement> 
inline 
void vector_erase(TContainer & container, TElement const& elem) 

    container.erase( std::remove(container.begin(), container.end(), elem), container.end() ); 


template 
<class TContainer, class TPred> 
inline 
void vector_erase_if(TContainer & container, TPred pred) 

    container.erase( std::remove_if(container.begin(), container.end(), pred), container.end() ); 


/// for list, set, map 
template <class TContainer, class TElement> 
inline
void list_erase(TContainer & container, TElement const& elem) 

    
for (TContainer::iterator it = container.begin(); it != container.end();)
    {
        
if (*it == elem)
        {
            container.erase(it
++);
        }
        
else
        {
            
++it;
        }
    }


template 
<class TContainer, class TPred> 
inline
void list_erase_if(TContainer & container, TPred pred) 

    
for (TContainer::iterator it = container.begin(); it != container.end();)
    {
        
if (pred(*it))
        {
            container.erase(it
++);
        }
        
else
        {
            
++it;
        }
    }


int _tmain(int argc, _TCHAR* argv[])
{
    typedef std::map
<int, std::string> tMap;
    typedef tMap::iterator tMapIterator;

    tMap MyMap;

    std::
string str = "I'm the first!";
    MyMap.insert(tMap::value_type(
0, str));

    str 
= "I'm the second!";
    MyMap.insert(tMap::value_type(
1, str));

    std::for_each(MyMap.begin(), MyMap.end(), stPrintPairContainerElement
< std::pair<int, std::string> >());

    list_erase_if( MyMap, std::bind2nd(stPred
< std::pair<int, std::string>, std::string >(), str) );

    std::cout 
<< "After erase: " << std::endl;
    std::for_each(MyMap.begin(), MyMap.end(), stPrintPairContainerElement
< std::pair<int, std::string> >());

    
/// for vector
    typedef std::vector<int> tVector;
    typedef tVector::iterator tVectorIterator;

    tVector MyVec;
    MyVec.push_back(
1);
    MyVec.push_back(
2);
    MyVec.push_back(
3);

    std::cout 
<< "Before erase: " << std::endl;
    std::for_each(MyVec.begin(), MyVec.end(), stPrintNoPairContainerElement
<int>());

    vector_erase(MyVec, 
1);

    std::cout 
<< "After erase: " << std::endl;
    std::for_each(MyVec.begin(), MyVec.end(), stPrintNoPairContainerElement
<int>());
    

    
return 0;
}

另一种写法:
这个erase返回指向被删除元素的下一个位置,所以不用再++了
template <class TContainer, class TPred> 
inline
void list_erase_if(TContainer & container, TPred pred) 

    
for (TContainer::iterator it = container.begin(); it != container.end();)
    {
        {
            std::cout 
<< it->first << " : " << it->second << std::endl;
            
if (pred(*it))
            {
                it 
= container.erase(it);
            }
            
else
            {
                
++it;
            }
        }
    }
posted on 2007-04-05 00:12 阿来 阅读(398) 评论(6)  编辑 收藏 引用 所属分类: c/c++
酝酿了很久的双分派器(Double Dispatcher)-- fixed version:【上一篇】
【翻译】Effective C++ (第2项:尽量使用const、enum、inline,避免使用 #define):【下一篇】
【相关文章】
  • Facade forward
  • 解决正版DB2ESE for aix为试用版问题
  • Solaris ABC for Linux/UnixDevelopers(II)
  • Oracle9i for SUN Solaris installation.
  • 建立交叉编译器 for arm (binutils-2.17 gcc-3.4.6 glibc-2.3.6)
  • oracle10g for linux 安装
  • [Linux]Linux常用软件for PCer
  • nmap的用法
  • 木马群cmdbcs.exe,wsttrs.exe,msccrt.exe,winform.exe,upxdnd.exe手工杀毒方法
  • 类似SourceForge的软件协作开发平台
  • 【随机文章】
  • session使用问题
  • 如何使用Ajax技术开发Web应用程序(2)
  • 用RSVP协议保证VoIP质量
  • 在window2k&XP下屏蔽Ctrl+Alt+del
  • Kernel 2.6x 编译过程
  • 用简单的命令检查电脑是否被安装木马
  • PC主板故障维修技巧
  • EclipseWork Plugin的安装!
  • 投诉申告:凭什么考驾照要高收费,考试管理却那么不严格!
  • SUSE FAQ 系列 -- 文件系统备份和恢复
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.