随笔 - 505  文章 - 1034  trackbacks - 0
<2018年7月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234


子曾经曰过:编程无他,唯手熟尔!

常用链接

留言簿(94)

随笔分类(649)

随笔档案(505)

相册

BCB

Crytek

  • crymod
  • Crytek's Offical Modding Portal

Game Industry

OGRE

other

Programmers

Qt

WOW Stuff

搜索

  •  

积分与排名

  • 积分 - 893946
  • 排名 - 14

最新随笔

最新评论

阅读排行榜

评论排行榜


不多说了,看代码
#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 七星重剑 阅读(7891) 评论(7)  编辑 收藏 引用 所属分类: 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;
}
}
}
}   回复  更多评论
  
# re: 在for循环里对std::map进行元素移除[未登录] 2013-07-10 15:37 master
学习了,赞一个  回复  更多评论
  
# re: 在for循环里对std::map进行元素移除 2018-07-12 00:41 七星重剑
过了11年多,再回来复习下,哈哈  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理