• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 505  文章 - 1034  trackbacks - 0
            <2009年8月>
            2627282930311
            2345678
            9101112131415
            16171819202122
            23242526272829
            303112345


            子曾經(jīng)曰過:編程無他,唯手熟爾!

            常用鏈接

            留言簿(94)

            隨筆分類(649)

            隨筆檔案(505)

            相冊

            BCB

            Crytek

            • crymod
            • Crytek's Offical Modding Portal

            Game Industry

            OGRE

            other

            Programmers

            Qt

            WOW Stuff

            搜索

            •  

            積分與排名

            • 積分 - 911296
            • 排名 - 14

            最新隨筆

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜


            不多說了,看代碼
            #include <iostream>
            #include 
            <string>
            #include 
            <map>
            #include 
            <algorithm>

            template
            <class _Ty>
            struct stPrintElement
                : 
            public std::unary_function<_Ty, void>
            {
                
            void operator()( const _Ty& Arg )
                {
                    std::cout 
            << Arg.second << std::endl;
                }
            };


            int _tmain(int argc, _TCHAR* argv[])
            {
                typedef std::map
            <int, std::string> tMap;
                typedef tMap::iterator tMapIterator;

                tMap MyMap;

                std::
            string str = "I'm the first!";
                MyMap.insert(tMap::value_type(
            0, str));

                str 
            = "I'm the second!";
                MyMap.insert(tMap::value_type(
            1, str));

                std::for_each(MyMap.begin(), MyMap.end(), stPrintElement
            < std::pair<int, std::string> >());

                
            for (tMapIterator it = MyMap.begin(); it != MyMap.end();)
                {
                    
            if (it->second == str)
                    {
                        MyMap.erase(it
            ++); /// Really smart! :-)
                    }
                    
            else
                    {
                        
            ++it;
                    }
                }

                std::cout 
            << "After erase: " << std::endl;
                std::for_each(MyMap.begin(), MyMap.end(), stPrintElement
            < std::pair<int, std::string> >());

                
            return 0;
            }

            后綴++解決了問題,哈哈
            這個(gè)在《c++標(biāo)準(zhǔn)程序庫》里有介紹,一直沒有用到這個(gè),今天用到了

            2007/04/05 重構(gòu):
            看了小明同志的回復(fù)后,優(yōu)化下
            #include <iostream>
            #include 
            <string>
            #include 
            <map>
            #include 
            <algorithm>
            #include 
            <vector>

            template
            <class TElement>
            struct stPrintPairContainerElement
                : 
            public std::unary_function<TElement, void>
            {
                
            void operator()( const TElement& elem )
                {
                    std::cout 
            << elem.first
                        
            << " : "
                        
            << elem.second 
                        
            << std::endl;
                }
            };

            template
            <class TElement>
            struct stPrintNoPairContainerElement
                : 
            public std::unary_function<TElement, void>
            {
                
            void operator()( const TElement& elem ) const
                {
                    std::cout 
            << elem << std::endl;
                }
            };

            template
            <class TLeft, class TRight>
            struct stPred
                : 
            public std::binary_function<TLeft, TRight, bool>
            {
                
            bool operator()( const TLeft& left , const TRight& right) const /// 最后這個(gè)const不加不行
                {
                    
            return left.second == right;
                }
            };

            /// for vector, deque 
            template <class TContainer, class TElement> 
            inline 
            void vector_erase(TContainer & container, TElement const& elem) 

                container.erase( std::remove(container.begin(), container.end(), elem), container.end() ); 


            template 
            <class TContainer, class TPred> 
            inline 
            void vector_erase_if(TContainer & container, TPred pred) 

                container.erase( std::remove_if(container.begin(), container.end(), pred), container.end() ); 


            /// for list, set, map 
            template <class TContainer, class TElement> 
            inline
            void list_erase(TContainer & container, TElement const& elem) 

                
            for (TContainer::iterator it = container.begin(); it != container.end();)
                {
                    
            if (*it == elem)
                    {
                        container.erase(it
            ++);
                    }
                    
            else
                    {
                        
            ++it;
                    }
                }


            template 
            <class TContainer, class TPred> 
            inline
            void list_erase_if(TContainer & container, TPred pred) 

                
            for (TContainer::iterator it = container.begin(); it != container.end();)
                {
                    
            if (pred(*it))
                    {
                        container.erase(it
            ++);
                    }
                    
            else
                    {
                        
            ++it;
                    }
                }


            int _tmain(int argc, _TCHAR* argv[])
            {
                typedef std::map
            <int, std::string> tMap;
                typedef tMap::iterator tMapIterator;

                tMap MyMap;

                std::
            string str = "I'm the first!";
                MyMap.insert(tMap::value_type(
            0, str));

                str 
            = "I'm the second!";
                MyMap.insert(tMap::value_type(
            1, str));

                std::for_each(MyMap.begin(), MyMap.end(), stPrintPairContainerElement
            < std::pair<int, std::string> >());

                list_erase_if( MyMap, std::bind2nd(stPred
            < std::pair<int, std::string>, std::string >(), str) );

                std::cout 
            << "After erase: " << std::endl;
                std::for_each(MyMap.begin(), MyMap.end(), stPrintPairContainerElement
            < std::pair<int, std::string> >());

                
            /// for vector
                typedef std::vector<int> tVector;
                typedef tVector::iterator tVectorIterator;

                tVector MyVec;
                MyVec.push_back(
            1);
                MyVec.push_back(
            2);
                MyVec.push_back(
            3);

                std::cout 
            << "Before erase: " << std::endl;
                std::for_each(MyVec.begin(), MyVec.end(), stPrintNoPairContainerElement
            <int>());

                vector_erase(MyVec, 
            1);

                std::cout 
            << "After erase: " << std::endl;
                std::for_each(MyVec.begin(), MyVec.end(), stPrintNoPairContainerElement
            <int>());
                

                
            return 0;
            }

            另一種寫法:
            這個(gè)erase返回指向被刪除元素的下一個(gè)位置,所以不用再++了
            template <class TContainer, class TPred> 
            inline
            void list_erase_if(TContainer & container, TPred pred) 

                
            for (TContainer::iterator it = container.begin(); it != container.end();)
                {
                    {
                        std::cout 
            << it->first << " : " << it->second << std::endl;
                        
            if (pred(*it))
                        {
                            it 
            = container.erase(it);
                        }
                        
            else
                        {
                            
            ++it;
                        }
                    }
                }
            posted on 2007-04-05 00:12 七星重劍 閱讀(8022) 評(píng)論(7)  編輯 收藏 引用 所屬分類: PL--c/c++C++ lib -- STL

            FeedBack:
            # re: 在for循環(huán)里對(duì)std::map進(jìn)行元素移除 2007-04-05 10:34 小明
            # re: 在for循環(huán)里對(duì)std::map進(jìn)行元素移除 2007-04-05 13:51 阿來
            @小明
            very impressive :)  回復(fù)  更多評(píng)論
              
            # re: 在for循環(huán)里對(duì)std::map進(jìn)行元素移除 2007-04-05 15:05 阿財(cái)
            Effective STL Item9有詳細(xì)介紹,包括遍歷各種容器的刪除介紹  回復(fù)  更多評(píng)論
              
            # re: 在for循環(huán)里對(duì)std::map進(jìn)行元素移除 2007-04-05 20:19 阿來
            @阿財(cái)
            下載了電子書,看看 :)  回復(fù)  更多評(píng)論
              
            # re: 在for循環(huán)里對(duì)std::map進(jìn)行元素移除 2007-04-06 15:04 ny
            記得應(yīng)這樣來的?
            it = erase(it);
              回復(fù)  更多評(píng)論
              
            # re: 在for循環(huán)里對(duì)std::map進(jìn)行元素移除 2007-04-06 21:04 阿來
            @ny
            對(duì),可以;放到帖子里了
            這個(gè)erase返回指向被刪除元素的下一個(gè)位置,所以不用再++了
            template <class TContainer, class TPred>
            inline
            void list_erase_if(TContainer & container, TPred pred)
            {
            for (TContainer::iterator it = container.begin(); it != container.end();)
            {
            {
            std::cout << it->first << " : " << it->second << std::endl;
            if (pred(*it))
            {
            it = container.erase(it);
            }
            else
            {
            ++it;
            }
            }
            }
            }   回復(fù)  更多評(píng)論
              
            # re: 在for循環(huán)里對(duì)std::map進(jìn)行元素移除[未登錄] 2013-07-10 15:37 master
            學(xué)習(xí)了,贊一個(gè)  回復(fù)  更多評(píng)論
              
            # re: 在for循環(huán)里對(duì)std::map進(jìn)行元素移除 2018-07-12 00:41 七星重劍
            過了11年多,再回來復(fù)習(xí)下,哈哈  回復(fù)  更多評(píng)論
              
            久久久久国产成人精品亚洲午夜| 亚洲国产精品综合久久网络 | 久久国产成人精品麻豆| 久久人人爽人人爽人人片AV麻豆| 婷婷综合久久狠狠色99h| 人人狠狠综合久久亚洲高清| www性久久久com| 亚洲国产精品无码久久久秋霞2 | 久久国产AVJUST麻豆| 国产免费福利体检区久久| 国产精品久久久久天天影视| 久久亚洲AV成人出白浆无码国产| 久久91精品国产91久久小草| 日本强好片久久久久久AAA| 2020国产成人久久精品| 日韩十八禁一区二区久久| 亚洲综合精品香蕉久久网97| 99久久国产亚洲高清观看2024| 国产日韩欧美久久| 曰曰摸天天摸人人看久久久| 国产高清国内精品福利99久久| 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲 | 亚洲va久久久噜噜噜久久天堂| 狠狠色综合网站久久久久久久高清 | 国产美女久久精品香蕉69| 久久久久高潮毛片免费全部播放| 久久综合狠狠色综合伊人| 国产精品久久久久AV福利动漫 | 久久99热狠狠色精品一区| 国产精品美女久久久久网| 99久久精品免费看国产| 国产午夜精品久久久久九九电影 | 一本久道久久综合狠狠爱| 麻豆成人久久精品二区三区免费 | 久久青青草原精品国产软件 | 亚洲精品无码久久一线| 亚洲日韩中文无码久久| 久久99精品国产99久久| 久久久久久国产精品无码下载| 久久精品国产99久久无毒不卡| 观看 国产综合久久久久鬼色 欧美 亚洲 一区二区|