//插入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()函数删除在pos位置的元素,或者删除在start和end之间的元素,或者删除那些值为key的所有元素 void erase( iterator pos ); void erase( iterator start, iterator end ); size_type erase( const KEY_TYPE &key );
/**
* @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;
}
/**
* @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;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有