多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國(guó)內(nèi)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁(yè) > php開源 > php教程 > 用STL設(shè)計(jì)消息隊(duì)列、優(yōu)先級(jí)消息隊(duì)列、資源分配管理器

用STL設(shè)計(jì)消息隊(duì)列、優(yōu)先級(jí)消息隊(duì)列、資源分配管理器

來(lái)源:程序員人生   發(fā)布時(shí)間:2014-10-13 08:24:13 閱讀次數(shù):3549次

          STL庫(kù)老早已經(jīng)成為C++的一部分,在使用C++開發(fā)項(xiàng)目的過程中,很多人還在猶豫要不要使用STL庫(kù),覺得STL庫(kù)很難,其實(shí)不然。我工作的項(xiàng)目中現(xiàn)在大量使用STL庫(kù),STL使用調(diào)試簡(jiǎn)單,高效,可以減少很多重復(fù)的代碼。

       本文的主要目的是使用STL的queue 和 priority queue來(lái)闡述下項(xiàng)目中經(jīng)常使用的消息隊(duì)列以及資源分配模式。本文的例子主要如下:

  • 消息隊(duì)列
  • 帶優(yōu)先級(jí)的消息隊(duì)列
  • 資源分配管理器

    STL容器

    我們將使用下面的容器來(lái)實(shí)現(xiàn)本文的例子:

     

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

     

        

     

    1.消息隊(duì)列

        消息隊(duì)列是項(xiàng)目中經(jīng)常需要使用的模式,下面使用STL的queue容器實(shí)現(xiàn):

      

    #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.優(yōu)先級(jí)消息隊(duì)列

          上面的消息隊(duì)列類只支持在隊(duì)列的尾部添加一個(gè)元素,在許多的應(yīng)用程序中,需要根據(jù)消息的優(yōu)先級(jí)將消息添加到隊(duì)列中,當(dāng)一個(gè)高優(yōu)先級(jí)的消息來(lái)了,需要將該消息添加到所有比它優(yōu)先級(jí)低的消息前面,下面將使用priority_queue來(lái)實(shí)現(xiàn)優(yōu)先級(jí)消息隊(duì)列類。

        

    函數(shù)對(duì)象(Functors)

    優(yōu)先級(jí)消息隊(duì)列的實(shí)現(xiàn)和上面消息隊(duì)列實(shí)現(xiàn)相似,唯一不同的是這里使用了函數(shù)對(duì)象來(lái)決定優(yōu)先權(quán)CompareMessages,該結(jié)構(gòu)重載了操作符"(,)",該機(jī)制可以實(shí)現(xiàn)可以將一個(gè)函數(shù)作為一個(gè)參數(shù),和函數(shù)指針對(duì)比有如下好處:

           1.函數(shù)對(duì)象消息更高,可以是內(nèi)聯(lián)函數(shù),函數(shù)指針總是有函數(shù)調(diào)用的開銷。

            2.函數(shù)對(duì)象提供了一個(gè)安全的實(shí)現(xiàn)方法,這樣的實(shí)現(xiàn)不會(huì)出現(xiàn)空指針訪問。

    #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兩個(gè)容器來(lái)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的資源分配器,用該容器來(lái)保存空閑的資源列表。

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

           1.Construction:當(dāng)這個(gè)資源分配器構(gòu)造出來(lái),需要給定空閑的資源列表,這些資源將添加到這個(gè)空閑資源列表。

           2.Allocate:當(dāng)需要一個(gè)資源時(shí),需要從空閑資源隊(duì)列中移除一個(gè)資源,并返回給調(diào)用者。

          3.Free:當(dāng)一個(gè)資源被釋放,需要將該資源加入空閑資源列表。

          4.GetFreeResourceCount:返回當(dāng)前可用的資源數(shù)目。

    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(); } };


     

     

     

生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 久久综合欧美成人 | 国内自拍成人网在线视频 | 亚洲欧美日韩久久精品第一区 | 国产做人爱三级视频在线 | 亚洲精品国产三级在线观看 | 亚洲乱码一区 | 欧美在线一二三 | www.亚洲精品.com| 欧美人与性动交α欧美精品图片 | 欧美一区二区三区视视频 | 中文字幕在线影院 | 福利在线看 | 亚洲精品高清中文字幕 | 91成人爽a毛片一区二区 | 国产内地激情精品毛片在线一 | 黑人性受xxxx黑人xyx性爽 | 性v天堂| 男人边吃奶边做性视频 | 国产成人精品综合久久久 | 欧美70一80老妇性大片 | 国产一区二区三区樱花动漫 | 美女福利在线 | 欧美日本道免费一区二区三区 | 好看欧美视频高清va | 就操成人网 | 一区二区三区在线视频观看 | 国产精品久久久久影院色老大 | 精品无码久久久久国产 | 五月天亚洲视频 | 亚洲人成综合在线播放 | 久久精品免看国产 | 久久天堂成人影院 | 最近最新手机中文字幕在线看 | 久久久91精品国产一区二区三区 | 久久五月天综合 | 欧美成人精品不卡视频在线观看 | 国产精品不卡高清在线观看 | 波多野结衣视频在线免费观看 | 色老久久精品偷偷鲁一区 | 黄色一区二区三区 | 18videosex欧美69|