STL查找时比较函数对象的实现

我们在使用STL的vector或list过程中经常要用到查找操作,对于需要排序的操作则需要找到插入点。如果我们放到这些容器中的对象是我们自己定义的话则需要自己实现比较函数。

我们这里举两个例子,分别是未排序的find_if和排好序的lower_bound:

假设我们有这样一个类:
class CUser
{
public:
 CUser(const std::string& strUserName);

 std::string& GetUserName()
 {
  return m_strUserName;
 }

private:
 std::string m_strUserName;  // User name
};
typedef vector<CUser*> TUser;
typedef vector<CUser*>::iterator TUserIt;

定义一个vector类:
TUser vctUser;

1. 我们想从无序vector中找到某一个用户的对象,可以这样做:
CUser* pUser = new CUser(string("lordong"));
TUserIt itFind = find_if(vctUser.begin(), vctUser.end(), is_needed_user<CUser*>(pUser));
if (itFind != vctUser.end())
{
 // 找到
}
// 决定是否需要释放pUser的资源

is_needed_user函数对象的实现代码:
template <typename PType>
class is_needed_user : std::unary_function<PType, bool>
{
public:
 is_needed_user(const PType pArg) : m_pValue(pArg) { }
 ~is_needed_user() { }

 bool operator()(const PType p) const
 {
  return _tcsicmp(m_pValue->GetUserName().c_str(), p->GetUserName().c_str()) == 0;
 }

private:
 PType m_pValue;
};

2. 我们想从有序的vector中找到某一对象,可以这样做:
CUser* pUser = new CUser(string("lordong"));
TUserIt itFind = lower_bound(vctUser.begin(), vctUser.end(), CompareUser<CUser*>());
if (itFind != vctUser.end())
{
 // 参见binary_search函数,注意参数顺序
 if (!CompareUser<CUser*>()(pUser, *itFound))
 {
  // 找到
 }
 else
 {
  // 找到插入点
 }
}
// 决定是否需要释放pUser的资源

CompareUser函数对象的实现代码:
template<typename Ty>
struct CompareUser : public std::binary_function<Ty, Ty, bool>
{
 bool operator()(const Ty& first, const Ty& second)
 {
  return _tcsicmp(first->GetUserName().c_str(), second->GetUserName().c_str()) < 0;
 }
};

 

Leave a Reply


提醒: 评论者允许使用'@user空格'的方式将自己的评论通知另外评论者。例如, ABC是本文的评论者之一,则使用'@ABC '(不包括单引号)将会自动将您的评论发送给ABC。请务必注意user必须和评论者名相匹配(大小写一致)。