惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
小众软件
小众软件
阮一峰的网络日志
阮一峰的网络日志
Recent Announcements
Recent Announcements
雷峰网
雷峰网
Last Week in AI
Last Week in AI
I
InfoQ
Google DeepMind News
Google DeepMind News
GbyAI
GbyAI
The Cloudflare Blog
aimingoo的专栏
aimingoo的专栏
量子位
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Blog — PlanetScale
Blog — PlanetScale
F
Full Disclosure
D
DataBreaches.Net
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
MyScale Blog
MyScale Blog
美团技术团队
V
V2EX
Jina AI
Jina AI
T
The Blog of Author Tim Ferriss
T
Tailwind CSS Blog
MongoDB | Blog
MongoDB | Blog
腾讯CDC
Vercel News
Vercel News
A
About on SuperTechFans
J
Java Code Geeks
Martin Fowler
Martin Fowler
V
Visual Studio Blog
博客园_首页
酷 壳 – CoolShell
酷 壳 – CoolShell
Recorded Future
Recorded Future
M
MIT News - Artificial intelligence
WordPress大学
WordPress大学
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
U
Unit 42
Engineering at Meta
Engineering at Meta
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
人人都是产品经理
人人都是产品经理
Y
Y Combinator Blog
云风的 BLOG
云风的 BLOG
博客园 - 三生石上(FineUI控件)
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The GitHub Blog
The GitHub Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 荣-

学习实践:使用模式,原则实现一个C++自动化测试程序 学习实践:使用模式,原则实现一个C++数据库访问类 C++字符转换等常用方法 DLL内存管理模板类 字符串处理代码(国际化转换C++版) 我的C++数据库访问库--临界区处理类 我的C++数据库访问库 批处理文件的学习 取得MySQL数据库表,列信息的SQL语句 我的我的C#数据库操作类(与大家交流) ACE项目的重构整理 安装SQL Server 2000和sp补丁时,安装程序提示"以前的某个程序安装已在安装计算机上创建挂起的文件操作。运行安装程序之前必须重新启动计算机"。 我的计算机安装步骤 创建oracle用户 我的重构步骤 我的C++代码检查列表 PowerDesigner高级应用 文档的阅读 如何设计类
C++中,以类成员函数指针作为参数对std::map中的元素进行迭代处理
荣- · 2011-07-21 · via 博客园 - 荣-

C++中使用Map会遇到迭代Map中元素的问题,使用for循环迭代元素,无形中增加了一层括号;使用函数指针调用类成员函数时,通常做法是,提供一个静态函数作为函数指针指向的函数,在静态函数中提供类指针对成员函数的调用。下面的代码通过foreach模板函数提供解决这两种问题的一个实例。

#pragma once

#include <map>

/*

托管Map的类,Map中存储着对象的指针

*/

template<typename S>

class XMRList

{

    // 重定义Map的类型别名,迭代器的类型别名,易于阅读

public:

    typedef std::map<long, S*> MyMAP;

    typedef typename MyMAP::iterator MyIterator;

    typedef typename MyMAP::const_iterator MyConstIterator;

    // 构造函数

public:

    XMRList()  {   }

    ~XMRList() {this->ReslaseList();}

public:

    // Map中查询对象,查询失败时,返回空指针

    S* Find(long id)

    {

       MyIterator it = this->m_Map.find(id);

       if (it != this->m_Map.end())

       {

           return it->second;

       }

       return NULL;

    }

    // Map中查询对象,查询失败时,返回空指针

    S* Find(long id) const

    {

       MyConstIterator it = this->m_Map.find(id);

       if (it != this->m_Map.end())

       {

           return it->second;

       }

       return NULL;

    }

    // 取得Map的大小

    UINT GetCount() const

    {

       return this->m_Map.size();

    }

    MyIterator Begin()

    {

       return this->m_Map.begin();

    }

    MyConstIterator Begin() const

    {

       return this->m_Map.begin();

    }

    MyIterator End()

    {

       return this->m_Map.end();

    }

    MyConstIterator End() const

    {

       return this->m_Map.end();

    }

public:

    // 遍历Map中的对象,并调用相关类成员函数(1个参数)进行处理

    template<typename T, typename R>

       void   foreach(T& o, R (T::* f)(S*))

    {

       MyIterator begin = this->m_Map.begin();

       MyIterator end = this->m_Map.end();

       for (; begin != end; ++begin)

       {

           (o.*f)(begin->second);

       }

    }

    // 遍历Map中的对象,并调用相关类成员函数(2个参数)进行处理

    template<typename T, typename R, typename A>

       void foreach(T& o, A arg, R (T::* f)(S*, A))

    {

       MyIterator begin = this->m_Map.begin();

       MyIterator end = this->m_Map.end();

       for (; begin != end; ++begin)

       {

           (o.*f)(begin->second, arg);

       }

    }

    // 遍历Map中的对象,并调用相关类成员函数(3个参数)进行处理

    template<typename T, typename R, typename A2, typename A3>

       void foreach(T& o, A2 arg2, A3 arg3, R (T::* f)(S*, A2, A3))

    {

       MyIterator begin = this->m_Map.begin();

       MyIterator end = this->m_Map.end();

       for (; begin != end; ++begin)

       {

           (o.*f)(begin->second, arg2, arg3);

       }

    }

    // 遍历Map中的对象,并调用相关类成员函数(4个参数)进行处理

    template<typename T, typename R, typename A2, typename A3, typename A4>

       void foreach(T& o, A2 arg2, A3 arg3, A4 arg4, R (T::* f)(S*, A2, A3, A4))

    {

       MyIterator begin = this->m_Map.begin();

       MyIterator end = this->m_Map.end();

       for (; begin != end; ++begin)

       {

           (o.*f)(begin->second, arg2, arg3, arg4);

       }

    }

public:

    // 为减少对map的依赖,提供的遍历map的函数

    // foreach处理不了模板函数的问题,所以提供该函数

    bool GetNextItor(MyIterator& itor)

    {

       if (itor == this->m_Map.end())

       {

           itor = this->m_Map.begin();

           return true;

       }

       ++itor;

       if (itor == this->m_Map.end())

       {

           return false;

       }

       return true;

    }

    // 为减少对map的依赖,提供的遍历map的函数

    // foreach处理不了模板函数的问题,所以提供该函数

    bool GetNextItor(MyConstIterator& itor) const

    {

       if (itor == this->m_Map.end())

       {

           itor = this->m_Map.begin();

           return true;

       }

       ++itor;

       if (itor == this->m_Map.end())

       {

           return false;

       }

       return true;

    }

private:

    void ReslaseList()       // 释放集合中的数据

    {

       for (MyIterator it = this->m_Map.begin(); it != this->m_Map.end(); ++it)

       {

           delete it->second;

           it->second = NULL;

       }

       this->m_Map.clear();

    }

public:

    MyMAP m_Map;  // 托管的集合

};

函数的调用:

class CStateBase

{

public:

    long m_ID;

}

class MapTest

{

public:

    XMRList<CStateBase>  m_MapProxy;

public:

    void OnForeach(CStateBase* pState)

    {

       TRACE("遍历Map元素,编号:%d\n", pState->m_ID);

       // ......

    }

    void TestForEach()

    {

       this->m_MapProxy.foreach(*this, &MapTest::OnForeach);

    }

};