该文档举例说明了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