The project needs to adopt a one-to-many mapping relationship, the original has not used Multimap, learning a bit, feeling more easily than imagined, a small example, demonstrating the use of multimap.
#include <map>
#include <iostream>
using namespace Std;
void Test_multimap ()
{
Construct test data for Multimap
Multimap<string, string> example;
Example.insert (Make_pair ("A"), String ("11"));
Example.insert (Make_pair ("A"), String ("22"));
Example.insert (Make_pair ("A"), String ("33"));
Example.insert (Make_pair (String ("B"), String ("44"));
Example.insert (Make_pair (String ("B"), String ("55"));
Example.insert (Make_pair (String ("B"), String ("66"));
Example.insert (Make_pair (String ("C"), String ("77"));
Example.insert (Make_pair (String ("C"), String ("88"));
Example.insert (Make_pair (String ("C"), String ("99"));
Example.insert (Make_pair ("D"), String ("000"));
Example.insert (Make_pair ("D"), String ("111"));
Example.insert (Make_pair ("D"), String ("222"));
Example.insert (Make_pair (String ("F"), String ("0000"));
Example.insert (Make_pair (String ("F"), String ("1111"));
Example.insert (Make_pair (String ("F"), String ("2222"));
Example.insert (Make_pair (String ("F"), String ("3333"));
std::string Del_item ("A");
Multimap<string,string>::size_type count = Example.erase (Del_item);
std::string Search_item = "C";
Multimap<string, String>::iterator iter = Example.find (Search_item);
Multimap<string, String>::size_type all = Example.count (Search_item);
Traverse with a specified number of
For (multimap<string, string>::size_type cnt = 0; cnt < all; ++cnt, ++iter)
{
std::cout<<iter->first<< ":" <<iter->second<<endl;
}
Iterates through a specified range of iterators
Search_item = "B";
for (iter = Example.lower_bound (Search_item); ITER!= Example.upper_bound (search_item); ++iter)
{
std::cout<<iter->first<< ":" <<iter->second<<endl;
}
Use a pair iterator to traverse an element
Search_item = "D";
Pair<multimap<string, string>::iterator,multimap<string, string>::iterator> pos;
for (pos = Example.equal_range (search_item);p os.first!= Pos.second;++pos.first)
{
std::cout<<pos.first->first<< ":" <<pos.first->second<<endl;
}
Delete element in Multimap
Search_item = "F";
for (iter = Example.begin (); Iter!= example.end ();)
{
if (Iter->second = = "0000" | | iter->second = "2222")
{
std::cout<< "Erase" <<iter->first<< ":" <<iter->second<<endl;
Example.erase (iter++);
}
Else
{
iter++;
}
}
Output all elements
for (iter = Example.begin (); Iter!= example.end (); ++iter)
{
std::cout<<iter->first<< ":" <<iter->second<<endl;
}
See the Multimap help file and don't see the definition of multimap::operator[], so you should not support
This access, even if supported, is not a good way to define the values it returns, such as what map defines.
type& operator[] (const key& _key) Multimap bad definition
}
int main (int argc,char *argv[])
{
Test_multimap ();
return 0;
}
Output:
C:77
c:88
c:99
B:44
B:55
b:66
d:000
d:111
d:222
Erase f:0000
Erase f:2222
B:44
B:55
b:66
C:77
c:88
c:99
d:000
d:111
d:222
f:1111
f:3333