国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++如何刪除map容器中指定值的元素詳解

2020-01-26 14:04:12
字體:
供稿:網(wǎng)友

前言

大家都知道m(xù)ap容器是C++ STL中的重要一員,平時會遇到刪除map容器中value為指定元素的問題,例如刪除所有字符串為"123"或者能整除3的元素。

一、map容器下的方法說明

由于map容器下的方法較多,這里只列舉代碼中用到的幾個方法:

insert()方法:

//插入val到pos的后面,然后返回一個指向這個元素的迭代器iterator insert( iterator pos, const pair<KEY_TYPE,VALUE_TYPE> &val );//插入start到end的元素到map中void insert( input_iterator start, input_iterator end );//只有在val不存在時插入val。返回值是一個指向被插入元素的迭代器和一個描述是否插入的bool值pair<iterator, bool> insert( const pair<KEY_TYPE,VALUE_TYPE> &val );

erase()方法:

//erase()函數(shù)刪除在pos位置的元素,或者刪除在start和end之間的元素,或者刪除那些值為key的所有元素void erase( iterator pos );void erase( iterator start, iterator end );size_type erase( const KEY_TYPE &key );

iterator迭代器。

二、刪除map容器中指定的字符串

下面代碼中map容器的value對應(yīng)的是一個string類型的指針,在初始化時類似于string *p = new string("123"); 。

/** * @FileName map_del_str.cpp * @Describe A simple example for deleting an element of string in map. * @Author  vfhky 2016-06-26 10:26 https://typecodes.com/cseries/mapdelintstring.html * @Compile  g++ map_del_str.cpp -o map_del_str * @Reference  */#include <iostream>#include <map>using namespace std;#define TOTAL 10#define DEL_STR "123"/** * 刪除map中所有元素為str的數(shù)據(jù) */void fun( map<int, string *> &map1, const string str ){ map<int, string *>::iterator it; int i_Total = 0; for( it=map1.begin(); it!=map1.end(); ) {  if( *(it->second) == str )  {   /**    * 123 123 123 123 123 123 123 123 123 123     */   cout << *(it->second) << " ";   //一定要先釋放內(nèi)存的控制   delete it->second;   it->second = NULL;   //再刪除迭代   map1.erase(it++);   ++i_Total;  }  else  {   it++;  } } //i_Total=[10] cout << endl << "i_Total=[" << i_Total << "]" << endl;}int main( int argc, char **argv ){ map<int, string *> map1; //初始化map1 for( int i=0; i<TOTAL; i++ ) {  map1.insert( pair<int, string *>(i,new string("123")) );  //map1[i] = new string("123"); } //刪除為DEL_STR的元素 fun( map1, DEL_STR ); //查看最后的數(shù)據(jù) map<int, string *>::iterator it1; for( it1=map1.begin(); it1!=map1.end(); ++it1 ) {  cout << "map1[" << it1->first << "]=[" << *(it1->second) << "]" << endl; } return 0;}

效果如下圖所示:

三、刪除map容器中指定的整型數(shù)據(jù)

下面代碼中map容器的value對應(yīng)的是一個int數(shù)據(jù),在初始化時可以直接使用map1[i] = i語句。

/** * @FileName map_del_int.cpp * @Describe A simple example for deleting an element of interger in map. * @Author  vfhky 2016-06-26 10:26 https://typecodes.com/cseries/mapdelintstring.html * @Compile  g++ map_del_int.cpp -o map_del_int * @Reference  */#include <iostream>#include <map>using namespace std;#define TOTAL 100#define DEL_INT 3/** * 刪除map中所有值整除NUM的元素 */void fun( map<int,int> &map1, const int NUM ){ map<int, int>::iterator it; int i_Total = 0; for( it=map1.begin(); it!=map1.end(); ) {  if( it->second % NUM == 0 )  {   /**    * 0 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96 99     */   cout << it->second << " ";   map1.erase(it++);   ++i_Total;  }  else  {   it++;  } } cout << endl << "i_Total=[" << i_Total << "]" << endl;}int main( int argc, char **argv ){ map<int, int> map1; //初始化map1 for( int i=0; i<TOTAL; i++ ) {  map1.insert(pair<int, int>(i,i));  //map1[i] = i; } //刪除整除3的元素 fun( map1, DEL_INT ); //查看最后的數(shù)據(jù) map<int, int>::iterator it1; for( it1=map1.begin(); it1!=map1.end(); ++it1 ) {  cout << "map1[" << it1->first << "]=[" << it1->second << "]" << endl; } return 0;}

效果如下圖所示:

四、附錄

STL容器分順序容器Sequence Container(包含vector,deque,list容器)和關(guān)聯(lián)容器Associative Container(包含set,multiset,map,multimap容器)。C++標(biāo)準(zhǔn)中,Sequence Container的erase函數(shù)會返回iterator,但Associative Container不返回iterator。所以在小節(jié)2、小節(jié)3中使用map1.erase(it++)而不是直接map1.erase(it) 。

五、總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對武林網(wǎng)的支持。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 东至县| 富顺县| 玉门市| 临颍县| 元朗区| 柯坪县| 东兰县| 清涧县| 望江县| 新昌县| 平安县| 洛扎县| 邵阳县| 三门县| 大余县| 筠连县| 庆云县| 军事| 金秀| 太湖县| 潢川县| 波密县| 乌兰察布市| 元阳县| 工布江达县| 承德市| 澜沧| 大城县| 海伦市| 图们市| 湘潭县| 天门市| 蓬莱市| 芜湖县| 吉隆县| 通辽市| 睢宁县| 铜陵市| 金平| 台山市| 诏安县|