#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
map<int, string> map1;
multimap<int, string> multimap1;
map1.insert(map<int,string>::value_type(1,"One"));
map1.insert(map<int, string>::value_type(2, "Two"));
map1.insert(map<int, string>::value_type(3, "Three"));
map1.insert(make_pair(-1,"Minus One"));
map1.insert(pair<int,string>(1000,"Four"));
map1[100] = "Five";
cout << "map1 size:" << map1.size() << endl;
map<int, string>::const_iterator map_iter;
for (map_iter =map1.begin();map_iter !=map1.end();++map_iter)
{
cout << "key:" << map_iter->first << " value:" << map_iter->second.c_str() << endl;
}
cout << "---------------------------"<< endl;
multimap1.insert(multimap<int,string>::value_type(1,"One"));
multimap1.insert(multimap<int, string>::value_type(1, "One"));
multimap1.insert(multimap<int, string>::value_type(2, "Two"));
multimap1.insert(pair<int,string>(2,"Test"));
multimap1.insert(pair<int,string>(2,"Test2"));
multimap1.insert(make_pair<int,string>(3,"Test"));
cout << "multimap size:" << multimap1.size() << endl;
multimap<int, string>::const_iterator multimap_iter;
for (multimap_iter = multimap1.begin();multimap_iter != multimap1.end();++multimap_iter)
{
cout << "key:" << multimap_iter->first << " value:" << multimap_iter->second.c_str() << endl;
}
cout << "element count:" << multimap1.count(1) << endl;
cout << "-----------------------------------------" << endl;
multimap<int, string>::const_iterator iter2;
iter2 = multimap1.find(2);
if (iter2 != multimap1.end())
{
size_t element_count = multimap1.count(2);
for (int k=0;k<element_count;k++)
{
cout << "find: "<<k<<" " << iter2->first << " " << iter2->second.c_str() << endl;
++iter2;
}
}
else
{
cout << "not found..." << endl;
}
cout << "---------------------------------------------"<<endl;
if (multimap1.erase(2)>0)
{
cout << "delete element success..."<<endl;
}
multimap<int, string>::const_iterator multimap_iter1;
for (multimap_iter1 = multimap1.begin(); multimap_iter1 != multimap1.end(); ++multimap_iter1)
{
cout << "key:" << multimap_iter1->first << " value:" << multimap_iter1->second.c_str() << endl;
}
cout << "---------------------------------------------" << endl;
multimap<int, string>::iterator iter3 = multimap1.find(1);
if (iter3 != multimap1.end())
{
size_t element_count = multimap1.count(1);
for (int k=0;k<element_count;k++)
{
iter3 = multimap1.find(1);
multimap1.erase(iter3);
//++iter3;
cout << "delete success..." << endl;
}
}
cout << "---------------------------------------------" << endl;
multimap<int, string>::const_iterator multimap_iter2;
for (multimap_iter2 = multimap1.begin(); multimap_iter2 != multimap1.end(); ++multimap_iter2)
{
cout << "key:" << multimap_iter2->first << " value:" << multimap_iter2->second.c_str() << endl;
}
system("pause");
return 0;
}
======================================
map1 size:6
key:-1 value:Minus One
key:1 value:One
key:2 value:Two
key:3 value:Three
key:100 value:Five
key:1000 value:Four
---------------------------
multimap size:6
key:1 value:One
key:1 value:One
key:2 value:Two
key:2 value:Test
key:2 value:Test2
key:3 value:Test
element count:2
-----------------------------------------
find: 0 2 Two
find: 1 2 Test
find: 2 2 Test2
---------------------------------------------
delete element success...
key:1 value:One
key:1 value:One
key:3 value:Test
---------------------------------------------
delete success...
delete success...
---------------------------------------------
key:3 value:Test
请按任意键继续. . .