Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1028060
  • 博文数量: 288
  • 博客积分: 10306
  • 博客等级: 上将
  • 技术积分: 3182
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-12 17:00
文章分类

全部博文(288)

文章存档

2011年(19)

2010年(38)

2009年(135)

2008年(96)

我的朋友

分类: C/C++

2009-10-16 12:45:31

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 TUser;
typedef vector::iterator TUserIt;

定义一个vector类:
TUser vctUser;

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

is_needed_user函数对象的实现代码:
template
class is_needed_user : std::unary_function
{
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());
if (itFind != vctUser.end())
{
 // 参见binary_search函数,注意参数顺序
 if (!CompareUser()(pUser, *itFound))
 {
  // 找到
 }
 else
 {
  // 找到插入点
 }
}
// 决定是否需要释放pUser的资源

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

阅读(2293) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~