随笔 - 477  文章 - 813  trackbacks - 0
<2007年4月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345


子曾经曰过:编程无他,唯手熟尔!
feedsky
抓虾
pageflakes
Rojo
狗狗
google reader
bloglines
my yahoo
newsgator
netvibes
鲜果


Locations of visitors to this page

常用链接

留言簿(66)

随笔分类(616)

随笔档案(477)

相册

BCB

Game Industry

OGRE

other

Programmers

Qt

WOW Stuff

搜索

  •  

积分与排名

  • 积分 - 276884
  • 排名 - 5

最新随笔

最新评论

阅读排行榜

评论排行榜

60天内阅读排行


不多说了,看代码
#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 七星重剑 阅读(2579) 评论(6)  编辑 收藏 引用 所属分类: PL--c/c++C++ lib -- STL

FeedBack:
# re: 在for循环里对std::map进行元素移除 2007-04-05 10:34 小明
# re: 在for循环里对std::map进行元素移除 2007-04-05 13:51 阿来
@小明
very impressive :)  回复  更多评论
  
# re: 在for循环里对std::map进行元素移除 2007-04-05 15:05 阿财
Effective STL Item9有详细介绍,包括遍历各种容器的删除介绍  回复  更多评论
  
# re: 在for循环里对std::map进行元素移除 2007-04-05 20:19 阿来
@阿财
下载了电子书,看看 :)  回复  更多评论
  
# re: 在for循环里对std::map进行元素移除 2007-04-06 15:04 ny
记得应这样来的?
it = erase(it);
  回复  更多评论
  
# re: 在for循环里对std::map进行元素移除 2007-04-06 21:04 阿来
@ny
对,可以;放到帖子里了
这个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;
}
}
}
}   回复  更多评论
  


标题  
姓名  
主页
验证码 *
内容(提交失败后,可以通过“恢复上次提交”恢复刚刚提交的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
[使用Ctrl+Enter键可以直接提交]
.NET频道  博客园社区  闪存
网站导航: