青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

隨筆 - 505  文章 - 1034  trackbacks - 0
<2020年5月>
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456


子曾經曰過:編程無他,唯手熟爾!

常用鏈接

留言簿(94)

隨筆分類(649)

隨筆檔案(505)

相冊

BCB

Crytek

  • crymod
  • Crytek's Offical Modding Portal

Game Industry

OGRE

other

Programmers

Qt

WOW Stuff

搜索

  •  

積分與排名

  • 積分 - 921951
  • 排名 - 14

最新隨筆

最新評論

閱讀排行榜

評論排行榜


不多說了,看代碼
#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;
}

后綴++解決了問題,哈哈
這個在《c++標準程序庫》里有介紹,一直沒有用到這個,今天用到了

2007/04/05 重構:
看了小明同志的回復后,優化下
#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 /// 最后這個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;
}

另一種寫法:
這個erase返回指向被刪除元素的下一個位置,所以不用再++了
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 七星重劍 閱讀(8054) 評論(7)  編輯 收藏 引用 所屬分類: PL--c/c++C++ lib -- STL

FeedBack:
# re: 在for循環里對std::map進行元素移除 2007-04-05 10:34 小明
# re: 在for循環里對std::map進行元素移除 2007-04-05 13:51 阿來
@小明
very impressive :)  回復  更多評論
  
# re: 在for循環里對std::map進行元素移除 2007-04-05 15:05 阿財
Effective STL Item9有詳細介紹,包括遍歷各種容器的刪除介紹  回復  更多評論
  
# re: 在for循環里對std::map進行元素移除 2007-04-05 20:19 阿來
@阿財
下載了電子書,看看 :)  回復  更多評論
  
# re: 在for循環里對std::map進行元素移除 2007-04-06 15:04 ny
記得應這樣來的?
it = erase(it);
  回復  更多評論
  
# re: 在for循環里對std::map進行元素移除 2007-04-06 21:04 阿來
@ny
對,可以;放到帖子里了
這個erase返回指向被刪除元素的下一個位置,所以不用再++了
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;
}
}
}
}   回復  更多評論
  
# re: 在for循環里對std::map進行元素移除[未登錄] 2013-07-10 15:37 master
學習了,贊一個  回復  更多評論
  
# re: 在for循環里對std::map進行元素移除 2018-07-12 00:41 七星重劍
過了11年多,再回來復習下,哈哈  回復  更多評論
  
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
              亚洲大片在线| 欧美国产亚洲视频| 亚洲一区在线看| 裸体一区二区三区| 午夜宅男欧美| 欧美日韩在线观看一区二区| 亚洲黄页视频免费观看| 久久久久久久网站| 欧美一区免费视频| 国产精品视频久久久| 一本久道久久久| 亚洲精品一区二区三区婷婷月 | 欧美在线观看网址综合| av成人动漫| 欧美日本免费一区二区三区| 亚洲精品一二区| 亚洲国产欧美一区二区三区久久| 久久综合激情| 亚洲国产精品成人一区二区| 美日韩精品视频免费看| 久久久久久穴| 亚洲国内自拍| 亚洲经典一区| 欧美日韩在线视频首页| 亚洲一区二区三区视频| 亚洲色图自拍| 国产亚洲一区精品| 久久综合九色综合欧美狠狠| 久久久亚洲国产美女国产盗摄| 亚洲缚视频在线观看| 亚洲国产一区在线观看| 欧美日韩一本到| 欧美亚洲一区二区在线| 午夜精品久久久99热福利| 国产中文一区二区| 欧美国产91| 欧美三区视频| 欧美国产日韩一区二区三区| 9久草视频在线视频精品| 亚洲精品久久视频| 欧美性一区二区| 久久精品国产91精品亚洲| 久久久噜噜噜久久中文字幕色伊伊| 日韩视频精品| 亚洲男人av电影| 最近中文字幕日韩精品| 一本不卡影院| 亚洲激情另类| 亚洲视频视频在线| 亚洲国产视频直播| 久久蜜桃香蕉精品一区二区三区| 久久av最新网址| 亚洲精品综合| 亚洲欧美影院| 99热在线精品观看| 亚洲嫩草精品久久| 亚洲精品一二三| 久久成人资源| 亚洲午夜久久久久久尤物| 久久精彩视频| 亚洲综合精品自拍| 美乳少妇欧美精品| 欧美在线观看网址综合| 欧美黄色影院| 久久免费黄色| 欧美日韩一区自拍| 免费看成人av| 国产伦精品一区二区三区在线观看 | 欧美一区二区视频观看视频| 亚洲精品视频在线看| 亚洲一区二区视频在线| 91久久精品国产| 久久精品国产免费观看| 午夜精品久久久久久久白皮肤| 另类激情亚洲| 老妇喷水一区二区三区| 国产欧美精品在线| 亚洲欧洲一区二区在线播放| 在线成人激情视频| 欧美一级欧美一级在线播放| 亚洲一级高清| 欧美精品日韩www.p站| 老司机67194精品线观看| 国产日韩亚洲欧美综合| 亚洲性感激情| 亚洲欧美三级伦理| 国产精品高潮呻吟久久av黑人| 91久久精品日日躁夜夜躁国产| 伊人色综合久久天天五月婷| 午夜欧美不卡精品aaaaa| 欧美一区成人| 国产精品手机视频| 国产精品99久久久久久白浆小说| 亚洲校园激情| 国产精品theporn| 在线一区欧美| 午夜精品视频在线| 国产视频久久| 久久精品国产亚洲a| 久久综合网色—综合色88| 国产一区二区精品久久91| 欧美在线91| 久久综合狠狠综合久久综合88| 精品91在线| 蜜臀a∨国产成人精品| 亚洲电影第1页| 日韩亚洲视频在线| 国产精品va在线| 小黄鸭精品密入口导航| 久久亚洲图片| 亚洲激情网站| 国产精品a久久久久| 一区二区三区.www| 久久福利影视| 免费在线看成人av| 最新国产成人av网站网址麻豆| 女人天堂亚洲aⅴ在线观看| 亚洲国产精品成人精品| 亚洲网站视频| 国产综合色在线| 免费短视频成人日韩| 99精品国产热久久91蜜凸| 欧美一区二区三区四区在线| 好吊一区二区三区| 欧美日韩精品一区视频| 午夜精品久久99蜜桃的功能介绍| 奶水喷射视频一区| 99精品国产在热久久| 国产乱人伦精品一区二区| 久久尤物电影视频在线观看| 亚洲精品在线免费观看视频| 欧美一区二区三区四区在线| 在线欧美小视频| 欧美三级第一页| 久久精品在线免费观看| 亚洲日韩视频| 免费久久99精品国产自| 亚洲欧美激情诱惑| 亚洲国产精品国自产拍av秋霞| 欧美日韩一区自拍| 久久综合久色欧美综合狠狠 | 午夜久久99| 亚洲激情女人| 久久久高清一区二区三区| 一区二区高清视频| 国产有码一区二区| 欧美三级精品| 模特精品裸拍一区| 欧美一区二区三区日韩| 99视频超级精品| 亚洲国产高清高潮精品美女| 久久久久九九视频| 亚洲尤物在线视频观看| 亚洲人久久久| 永久久久久久| 国产一区二区你懂的| 国产精品mm| 欧美日韩视频专区在线播放 | 免费观看在线综合色| 午夜国产精品视频| 99精品视频免费全部在线| 欧美国产日本| 久久在线免费| 久久精品久久综合| 午夜精品国产| 亚洲素人一区二区| aa国产精品| 亚洲美女视频在线观看| 亚洲欧洲日本mm| 亚洲黄色在线| 亚洲精品一区二区三区在线观看| 伊人久久噜噜噜躁狠狠躁| 国外成人在线| 精品电影在线观看| 亚洲国产精品久久久久婷婷884| 国产亚洲精品高潮| 国产日韩在线亚洲字幕中文| 国产午夜亚洲精品不卡| 国语自产在线不卡| 激情综合电影网| 伊人狠狠色j香婷婷综合| 免费观看在线综合| 欧美va亚洲va日韩∨a综合色| 性刺激综合网| 亚洲欧美视频一区二区三区| 亚洲一区二区在线播放| 亚洲欧美日本日韩| 欧美与黑人午夜性猛交久久久| 久久国产欧美| 麻豆精品视频在线| 欧美高清视频一区| 亚洲精品国久久99热| av成人手机在线| 亚洲小视频在线| 久久国产精品黑丝| 农村妇女精品| 欧美丝袜一区二区| 国产麻豆视频精品| 影音先锋欧美精品| 亚洲一区二区三区四区中文|