该文档举例说明了multimap的查找和删除元素的使用
Posted 聚沙成塔
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了该文档举例说明了multimap的查找和删除元素的使用相关的知识,希望对你有一定的参考价值。
该文档举例说明了multimap的查找和删除元素的使用。
其中,在使用迭代器遍历元素的时候,如果使用了删除迭代器的操作,那么需要小心迭代器失效的情况。
/* 功能说明: multimap的查找和删除元素的使用举例 代码说明: 使用multimap的equal_range()方法来查找符合指定key的元素。使用erase来删除指定key的元素。 实现方式: 限制条件或者存在的问题: 无 */ #include <iostream> #include <string> #include <map> using namespace std; class CData { public: CData() { sequence = 0; 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[2048] = { 0 }; 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(11, strClassName); strClassName = "CData12"; CData *p12 = new CData(12, strClassName); strClassName = "CData13"; CData *p13 = new CData(13, strClassName); strClassName = "CData23"; CData *p23 = new CData(23, strClassName); strClassName = "CData24"; CData *p24 = new CData(24, strClassName); strClassName = "CData26"; CData *p26 = new CData(26, strClassName); multData.insert(make_pair("1", p11)); multData.insert(make_pair("2", p23)); multData.insert(make_pair("1", p13)); multData.insert(make_pair("2", p24)); multData.insert(make_pair("1", p12)); multData.insert(make_pair("2", 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("2"); for (multimap<string, CData*>::iterator itr = pitr2.first; itr != pitr2.second; itr++) { cout << itr->first << "\t" << (itr->second)->toString() << endl; } multData.erase("2"); pitr2 = multData.equal_range("2"); 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("1", p11)); multData.insert(make_pair("2", p23)); multData.insert(make_pair("1", p13)); multData.insert(make_pair("2", p24)); multData.insert(make_pair("1", p12)); multData.insert(make_pair("2", p26)); cout << "find element by key using another way" << endl; for (multimap<string, CData*>::iterator itr_find = multData.lower_bound("1"); itr_find != multData.upper_bound("1"); 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 == "1") { 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 0; }
程序的输出结果:
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的查找和删除元素的使用的主要内容,如果未能解决你的问题,请参考以下文章
查找 Mongodb 文档,如果它包含 mongoose 中的数组中的元素