使用reverse_iterator迭代器实现STL容器的反向遍历

来源:互联网 发布:应聘淘宝美工自我介绍 编辑:程序博客网 时间:2024/06/14 19:46

        STL中诸如vector、list等容器,使用起来安全、稳定、高效,给我们的日常开发带来了很多的便捷。某些时候因为一些特殊的需要,我们需要反向遍历容器,使用reverse_iterator、rbegin()、rend()即可实现,相关例子代码如下所示:

void CMsgLauncherListDlg::UpdateLauncherItemRect(){        CRect rcClient;        GetClientRect( &rcClient );        CRect rcItem;        u32 dwXPos = 0;        u32 dwYPos = 0;        int nItemCount = 0;        // 反向遍历说明:当前只能显示有限数量(LATEST_MSG_LAUNCHER_DISPLAY_NUM)的消息发起者,如果当前消息发起者        // 数量大于LATEST_MSG_LAUNCHER_DISPLAY_NUM,只显示LATEST_MSG_LAUNCHER_DISPLAY_NUM个最近的消息发起者,之        // 前的消息发起者在最近的被查看后才能显示,而最近的消息发起者是放在列表尾部的        std::vector<MsgLauncher*>::reverse_iterator it = m_tMsgLauncherList.rbegin();    for ( ; it != m_tMsgLauncherList.rend(); it++ )    {                nItemCount++;                if ( nItemCount > LATEST_MSG_LAUNCHER_DISPLAY_NUM ) // 超过LATEST_MSG_LAUNCHER_DISPLAY_NUM的,暂时不再显示                {                        rcItem.SetRect( 0, 0, 0, 0 );                        (*it)->SetRect( rcItem );                        continue;                }                rcItem.SetRect( dwXPos, dwYPos, dwXPos+rcClient.Width(), dwYPos+MSG_LAUNCHER_ITEM_HEIGHT );                (*it)->SetRect( rcItem );                (*it)->Draw( &m_memDC );  // 将消息发起者信息绘制到内存DC中        dwYPos += MSG_LAUNCHER_ITEM_HEIGHT;    }} 


0 0
原创粉丝点击