首页 > 编程 > C++ > 正文

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

2020-01-26 14:04:12
字体:
来源:转载
供稿:网友

前言

大家都知道map容器是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()函数删除在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对应的是一个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的数据 */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) << " ";   //一定要先释放内存的控制   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 ); //查看最后的数据 map<int, string *>::iterator it1; for( it1=map1.begin(); it1!=map1.end(); ++it1 ) {  cout << "map1[" << it1->first << "]=[" << *(it1->second) << "]" << endl; } return 0;}

效果如下图所示:

三、删除map容器中指定的整型数据

下面代码中map容器的value对应的是一个int数据,在初始化时可以直接使用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 ); //查看最后的数据 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容器)和关联容器Associative Container(包含set,multiset,map,multimap容器)。C++标准中,Sequence Container的erase函数会返回iterator,但Associative Container不返回iterator。所以在小节2、小节3中使用map1.erase(it++)而不是直接map1.erase(it)

五、总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对武林网的支持。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表