该文档举例说明了multimap的查找和删除元素的使用。

其中,在使用迭代器遍历元素的时候,如果使用了删除迭代器的操作,那么需要小心迭代器失效的情况。

/*
功能说明:
multimap的查找和删除元素的使用举例
代码说明:
使用multimap的equal_range()方法来查找符合指定key的元素。使用erase来删除指定key的元素。
实现方式:
限制条件或者存在的问题:

*/ #include <iostream>
#include <string>
#include <map> using namespace std; class CData
{
public:
CData()
{
sequence = ;
this->remark = "default string"; cout << "CData()\t" << toString() << "\t" << endl;
} CData(int i, string &s)
{
this->sequence = i;
this->remark = s; cout << "CData(int i,string &s)\t" << toString() << "\t" << endl;
} CData(const CData &data)
{
this->sequence = data.sequence;
this->remark = data.remark; cout << "CData(const CData &data)\t" << toString() << "\t" << endl;
} CData operator = (const CData &data)
{
this->sequence = data.sequence;
this->remark = data.remark; cout << "CData operator = (const CData &data)\t" << toString() << "\t" << endl; return *this;
} void setSequence(const int i)
{
this->sequence = i;
} void setRemark(const string &s)
{
this->remark = s;
} string toString() const
{
char tmp[] = { };
sprintf(tmp, "[sequence:%d | remark:%s @ %p]", this->sequence, this->remark.c_str(),this); //此处应该有内存复制操作,所以不用担心返回后tmp数组所指向的内存被修改或者不存在的情况。
return tmp;
} ~CData()
{
cout << "~CData()\t" << this << endl;
}
protected:
private:
int sequence;
string remark;
}; int main(int argc, char ** argv)
{
cout << "process begin @[" << (void*)&main << "]" << endl; multimap<string, CData *> multData;
string strClassName = "CData11";
CData *p11 = new CData(, strClassName);
strClassName = "CData12";
CData *p12 = new CData(, strClassName);
strClassName = "CData13";
CData *p13 = new CData(, strClassName);
strClassName = "CData23";
CData *p23 = new CData(, strClassName);
strClassName = "CData24";
CData *p24 = new CData(, strClassName);
strClassName = "CData26";
CData *p26 = new CData(, strClassName); multData.insert(make_pair("", p11));
multData.insert(make_pair("", p23));
multData.insert(make_pair("", p13));
multData.insert(make_pair("", p24));
multData.insert(make_pair("", p12));
multData.insert(make_pair("", p26)); cout << "multData.size()\t" << multData.size() << endl; cout << "show all msg" << endl;
// multimap在创建的时候,已经将元素根据key的顺序排好了(前提是key支持<比较)。
for (multimap<string, CData*>::iterator itr = multData.begin(); itr != multData.end(); itr++)
{
cout << itr->first << "\t" << (itr->second)->toString() << endl;
} cout << "find 2" << endl;
pair<multimap<string, CData*>::iterator, multimap<string, CData*>::iterator> pitr2 = multData.equal_range("");
for (multimap<string, CData*>::iterator itr = pitr2.first; itr != pitr2.second; itr++)
{
cout << itr->first << "\t" << (itr->second)->toString() << endl;
} multData.erase(""); pitr2 = multData.equal_range("");
if (pitr2.first == pitr2.second)
{
cout << "can not find key 2 after delete key 2" << endl;
} cout << "show all msg after delete 2" << endl;
for (multimap<string, CData*>::iterator itr = multData.begin(); itr != multData.end(); itr++)
{
cout << itr->first << "\t" << (itr->second)->toString() << endl;
} multData.insert(make_pair("", p11));
multData.insert(make_pair("", p23));
multData.insert(make_pair("", p13));
multData.insert(make_pair("", p24));
multData.insert(make_pair("", p12));
multData.insert(make_pair("", p26)); cout << "find element by key using another way" << endl;
for (multimap<string, CData*>::iterator itr_find = multData.lower_bound(""); itr_find != multData.upper_bound(""); itr_find++)
{
cout << itr_find->first << "\t" << (itr_find->second)->toString() << endl;
} cout << "delete key 1 by iterator" << endl;
// 这是一种错误的迭代器删除元素的方式。
/*
for (multimap<string, CData*>::iterator itr = multData.begin(); itr != multData.end(); itr++)
{
cout << itr->first << "\t" << (itr->second)->toString() << endl;
if (itr->first == "1")
{
multData.erase(itr);
}
}
*/ // 正确的使用迭代器删除元素的方式。
for (multimap<string, CData*>::iterator itr = multData.begin(); itr != multData.end(); )
{
if (itr->first == "")
{
cout <<"find key 1. "<< itr->first << "\t" << (itr->second)->toString() << endl;
itr = multData.erase(itr);
}
else
{
cout << "can not find key 1. " << itr->first << "\t" << (itr->second)->toString() << endl;
itr++;
}
} cout << "show all msg after erase key 1" << endl;
for (multimap<string, CData*>::iterator itr = multData.begin(); itr != multData.end(); itr++)
{
cout << itr->first << "\t" << (itr->second)->toString() << endl;
} delete p11;
delete p12;
delete p13;
delete p23;
delete p24;
delete p26; return ;
}

程序的输出结果:

process begin @[00B2188E]
CData(int i,string &s)  [sequence:11 | remark:CData11 @ 011FCFD0]
CData(int i,string &s)  [sequence:12 | remark:CData12 @ 011FD020]
CData(int i,string &s)  [sequence:13 | remark:CData13 @ 011FD8C0]
CData(int i,string &s)  [sequence:23 | remark:CData23 @ 011FE448]
CData(int i,string &s)  [sequence:24 | remark:CData24 @ 011FE6C8]
CData(int i,string &s)  [sequence:26 | remark:CData26 @ 011FE808]
multData.size() 6
show all msg
1       [sequence:11 | remark:CData11 @ 011FCFD0]
1       [sequence:13 | remark:CData13 @ 011FD8C0]
1       [sequence:12 | remark:CData12 @ 011FD020]
2       [sequence:23 | remark:CData23 @ 011FE448]
2       [sequence:24 | remark:CData24 @ 011FE6C8]
2       [sequence:26 | remark:CData26 @ 011FE808]
find 2
2       [sequence:23 | remark:CData23 @ 011FE448]
2       [sequence:24 | remark:CData24 @ 011FE6C8]
2       [sequence:26 | remark:CData26 @ 011FE808]
can not find key 2 after delete key 2
show all msg after delete 2
1       [sequence:11 | remark:CData11 @ 011FCFD0]
1       [sequence:13 | remark:CData13 @ 011FD8C0]
1       [sequence:12 | remark:CData12 @ 011FD020]
find element by key using another way
1       [sequence:11 | remark:CData11 @ 011FCFD0]
1       [sequence:13 | remark:CData13 @ 011FD8C0]
1       [sequence:12 | remark:CData12 @ 011FD020]
1       [sequence:11 | remark:CData11 @ 011FCFD0]
1       [sequence:13 | remark:CData13 @ 011FD8C0]
1       [sequence:12 | remark:CData12 @ 011FD020]
delete  key 1 by iterator
find key 1. 1   [sequence:11 | remark:CData11 @ 011FCFD0]
find key 1. 1   [sequence:13 | remark:CData13 @ 011FD8C0]
find key 1. 1   [sequence:12 | remark:CData12 @ 011FD020]
find key 1. 1   [sequence:11 | remark:CData11 @ 011FCFD0]
find key 1. 1   [sequence:13 | remark:CData13 @ 011FD8C0]
find key 1. 1   [sequence:12 | remark:CData12 @ 011FD020]
can not find key 1. 2   [sequence:23 | remark:CData23 @ 011FE448]
can not find key 1. 2   [sequence:24 | remark:CData24 @ 011FE6C8]
can not find key 1. 2   [sequence:26 | remark:CData26 @ 011FE808]
show all msg after erase key 1
2       [sequence:23 | remark:CData23 @ 011FE448]
2       [sequence:24 | remark:CData24 @ 011FE6C8]
2       [sequence:26 | remark:CData26 @ 011FE808]
~CData()        011FCFD0
~CData()        011FD020
~CData()        011FD8C0
~CData()        011FE448
~CData()        011FE6C8
~CData()        011FE808

该文档举例说明了multimap的查找和删除元素的使用的更多相关文章

  1. ELK学习总结(2-4)bulk 批量操作-实现多个文档的创建、索引、更新和删除

    bulk 批量操作-实现多个文档的创建.索引.更新和删除 ----------------------------------------------------------------------- ...

  2. 慧自文档:代替 Everything 来快速查找文件的,实现文件显示在文件夹的层次结构中

    1. 搜索功能和Everything一样快和强大 具有 Everything 搜索快.搜索功能强等优点, 解决了不能方便选择搜索哪个文件夹, 解决了不能同一个画面进行预览等问题 2.文件直接显示到文件 ...

  3. 用Java对xml文档进行遍历,更新,创建,删除

    import java.io.File;import java.io.FileInputStream; import javax.imageio.stream.FileImageInputStream ...

  4. jquery 追加元素/jquery文档处理,插入、修改、移动、删除指定的DOM元素.

    jquery 追加元素 $("#content").append("..."); // 添加到元素内部最后面 $("#content").p ...

  5. pytest文档40-pytest.ini配置用例查找规则(面试题)

    前言 面试题:pytest如何执行不是test开头的用例?如执行 xxx_*.py这种文件的用例. pytest.ini 配置文件可以修改用例的匹配规则. pytest命令行参数 cmd打开输入pyt ...

  6. HTML DOCTYPE文档类型举例说明

    HTML DOCTYPE文档类型举例说明 HTML4.01文档过渡定义类型,此类型定义的文档可以使用HTML中的标签与元素包括一些不被W3C推荐的标签(例如:font.b等),不可以使用框架 < ...

  7. DOM (Document Object Model)文档对象模型

    [理解下DOM] DOM——Document Object Mode.DOM是网页上XHTML中文档正文标题啊.段落.列表.样式.以及ID/class等所有其他数据的一个内部表示.我自己的理解是将网页 ...

  8. css3学习文档

    什么是CSS3? CSS3是CSS2的升级版本,3只是版本号,它在CSS2.1的基础上增加了很多强大的新功能. 目前主流浏览器chrome.safari.firefox.opera.甚至360都已经支 ...

  9. HTML5的文档结构

    HTML5的文档结构 HTML5简化了许多,它的设计遵循了3个原则:1.兼容性.2.实用性.3.通用访问性     1. header 元素     <header> 标签定义文档或者文档 ...

随机推荐

  1. HDU - 6370 Werewolf 2018 Multi-University Training Contest 6 (DFS找环)

    求确定身份的人的个数. 只能确定狼的身份,因为只能找到谁说了谎.但一个人是否是民,无法确定. 将人视作点,指认关系视作边,有狼边和民边两种边. 确定狼的方法只有两种: 1. 在一个仅由一条狼边组成的环 ...

  2. 分支语句(switch case)

    /switch case 的应用 Console.WriteLine("1.汉堡包"); Console.WriteLine("2.薯条"); Console. ...

  3. vue-scroller的使用

    一 安装 使用npm 安装 npm install vue-scroller -S 二 引入 https://www.jianshu.com/p/a39f5276ff0b https://www.np ...

  4. WPF MVVM模式下ComboBox级联效果 选择第一项

    MVVM模式下做的省市区的级联效果.通过改变ComboBox执行命令改变市,区. 解决主要问题就是默认选中第一项 1.首先要定义一个属性,继承自INotifyPropertyChanged接口.我这里 ...

  5. Java学习笔记之Java 继承中的构造方法

    参考

  6. CSS 3 中的多列属性

    .column-count <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  7. MySQL5.7导入数据报错ERROR 1067 (42000) at line 1015: Invalid default value for 'service_time'

    解决办法: 修改my.cnf,[mysqld] 下面添加sql_mode=STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_U ...

  8. 20145217《网络对抗》 Web安全基础实践

    20145217<网络对抗> Web安全基础实践 一.实践任务 本实践的目标理解常用网络攻击技术的基本原理.Webgoat实践下相关实验. 二.实验后回答问题 (1)SQL注入攻击原理,如 ...

  9. 剑指offer题解02-10

    02 单例模式 单例模式,是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例的特殊类.通过单例模式可以保证系统中,应用该模式的类一个类只有一个实例.即一个类只有一个对象实例. 从具体实现角 ...

  10. redis入门笔记

    redis入门笔记 参考redis实战手册 1. Redis在windows下安装 下载地址:https://github.com/MSOpenTech/redis/tags 安装Redis 1.1. ...