日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > php教程 > 用STL設計消息隊列、優先級消息隊列、資源分配管理器

用STL設計消息隊列、優先級消息隊列、資源分配管理器

來源:程序員人生   發布時間:2014-10-13 08:24:13 閱讀次數:3559次

          STL庫老早已經成為C++的一部分,在使用C++開發項目的過程中,很多人還在猶豫要不要使用STL庫,覺得STL庫很難,其實不然。我工作的項目中現在大量使用STL庫,STL使用調試簡單,高效,可以減少很多重復的代碼。

       本文的主要目的是使用STL的queue 和 priority queue來闡述下項目中經常使用的消息隊列以及資源分配模式。本文的例子主要如下:

  • 消息隊列
  • 帶優先級的消息隊列
  • 資源分配管理器

    STL容器

    我們將使用下面的容器來實現本文的例子:

     

    queue 隊列容器支持添加一個元素,并且從中刪除一個元素,也可以變成一個雙端隊列
    priority_queue 向隊尾添加一個新元素,如果該優先權大于前面的元素,將放在前面,元素的優先權由用戶指定的函數傳入。
    stack 有后進先出的特點,可以選擇選擇vetctor或其他線性列表放入容器。

     

        

     

    1.消息隊列

        消息隊列是項目中經常需要使用的模式,下面使用STL的queue容器實現:

      

    #include // STL header file for queue #include using namespace std; // Specify that we are using the std namespace class Message; class Message_Queue { typedef queue > MsgQueType; MsgQueType m_msgQueue; public: void Add(Message *pMsg) { // Insert the element at the end of the queue m_msgQueue.push(pMsg); } Message *Remove() { Message *pMsg = NULL; // Check if the message queue is not empty if (!m_msgQueue.empty()) { // Queue is not empty so get a pointer to the // first message in the queue pMsg = m_msgQueue.front(); // Now remove the pointer from the message queue m_msgQueue.pop(); } return pMsg; } int GetLength() const { return m_msgQueue.size(); } };


     

    2.優先級消息隊列

          上面的消息隊列類只支持在隊列的尾部添加一個元素,在許多的應用程序中,需要根據消息的優先級將消息添加到隊列中,當一個高優先級的消息來了,需要將該消息添加到所有比它優先級低的消息前面,下面將使用priority_queue來實現優先級消息隊列類。

        

    函數對象(Functors)

    優先級消息隊列的實現和上面消息隊列實現相似,唯一不同的是這里使用了函數對象來決定優先權CompareMessages,該結構重載了操作符"(,)",該機制可以實現可以將一個函數作為一個參數,和函數指針對比有如下好處:

           1.函數對象消息更高,可以是內聯函數,函數指針總是有函數調用的開銷。

            2.函數對象提供了一個安全的實現方法,這樣的實現不會出現空指針訪問。

    #include // STL header file for queue #include using namespace std; // Specify that we are using the std namespace class Message; class Priority_Message_Queue { struct Entry { Message *pMsg; int priority; }; struct Compare_Messages { bool operator () (const Entry& left , const Entry& right) { return (left.priority < right.priority); } }; typedef priority_queue, Compare_Messages > Message_Queue_Type; Message_Queue_Type m_message_Queue; public: void Add(Message *pMsg, int priority) { // Make an entry Entry entry; entry.pMsg = pMsg; entry.priority = priority; // Insert the element according to its priority m_message_Queue.push(entry); } Message *Remove() { Message *pMsg = NULL; // Check if the message queue is not empty if (!m_message_Queue.empty()) { // Queue is not empty so get a pointer to the // first message in the queue pMsg = (m_message_Queue.top()).pMsg; // Now remove the pointer from the message queue m_message_Queue.pop(); } return (pMsg); } size_t Get_Length() const { return m_message_Queue.size(); } };


         

    3.資源分配管理器

    這里使用queue 和 stack兩個容器來實現一個簡單的資源分配器,用該容器來保存空閑的資源列表。

    該資源分配器支持如下接口:

           1.Construction:當這個資源分配器構造出來,需要給定空閑的資源列表,這些資源將添加到這個空閑資源列表。

           2.Allocate:當需要一個資源時,需要從空閑資源隊列中移除一個資源,并返回給調用者。

          3.Free:當一個資源被釋放,需要將該資源加入空閑資源列表。

          4.GetFreeResourceCount:返回當前可用的資源數目。

    Coldest First(使用queue)

    #include // STL header file for queue #include using namespace std; // Specify that we are using the std namespace class Resource; class Cold_Resource_Allocator { typedef queue > Free_Queue_Type; Free_Queue_Type m_free_Resource_Queue; public: Cold_Resource_Allocator(int resource_Count, Resource *resource_Array[]) { for (int i = 0; i < resource_Count; i++) { m_free_Resource_Queue.push(resource_Array[i]); } } Resource * Allocate() { Resource *pResource = NULL; // Check if any free resources are available. if (!m_free_Resource_Queue.empty()) { // Queue is not empty so get a pointer to the // first resource in the queue pResource = m_free_Resource_Queue.front(); // Now remove the pointer from the free resource queue m_free_Resource_Queue.pop(); } return pResource; } void Free(Resource *pResource) { // Insert the resource at the end of the free queue m_free_Resource_Queue.push(pResource); } size_t GetFreeResourceCount() { return m_free_Resource_Queue.size(); } };


     

    Hottest First(使用statck)

    #include // STL header file for stack #include using namespace std; // Specify that we are using the std namespace class Resource; class Hot_Resource_Allocator { typedef stack > Free_Stack_Type; Free_Stack_Type m_free_Resource_Stack; public: Hot_Resource_Allocator(int resource_Count, Resource *resource_Array[]) { for (int i = 0; i < resource_Count; i++) { m_free_Resource_Stack.push(resource_Array[i]); } } Resource * Allocate() { Resource *pResource = NULL; // Check if any free resources are available. if (!m_free_Resource_Stack.empty()) { // Queue is not empty so get a pointer to the // first resource in the stack pResource = m_free_Resource_Stack.top(); // Now remove the pointer from the free resource stack m_free_Resource_Stack.pop(); } return pResource; } void Free(Resource *pResource) { // Insert the resource at the end of the free stack m_free_Resource_Stack.push(pResource); } size_t GetFreeResourceCount() { return m_free_Resource_Stack.size(); } };


     

     

     

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 国产一区二区三区免费观看 | 国产精品免费网站 | 国产高清中文字幕 | 亚洲91| 久久久精品久久久久 | 欧美专区一区 | 日韩在线| 天堂在线中文资源 | 欧美午夜一区 | 成人国产精品免费观看视频 | 欧美精品在线观看 | 黄色一级视频播放 | 久久久久久久国产 | 欧美精品一级二级三级 | 99精品欧美一区二区三区综合在线 | 中文字幕av片 | 成人在线黄色 | 在线播放国产一区二区三区 | 国产一区二区三区免费在线观看 | 国产精品精品视频一区二区三区 | 九九视频在线 | 久久亚洲国产精品 | 中文字幕日本视频 | 欧美一区二区三区久久精品 | 色先锋在线 | 一区二区免费在线视频 | www.av一区 | 久久人人爽人人爽人人片av不 | 亚洲一二三区在线 | 男女黄色网 | 久久精品国产一区二区三区不卡 | 一区二区毛片 | 成人日韩视频 | 亚洲欧美一区二区三区国产精品 | 日本 欧美 国产 | 亚洲午夜激情电影 | 在线视频综合 | 蜜臀av在线播放一区二区三区 | 性av在线| 欧美激情视频在线观看 | 国产精品免费看 |