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