清除单map(非嵌套map)
#include<map>
#include<string>
#include<iostream>
using namespace std;
int main()
{
map<int, string> m_in;
map<int,string>::iterator it_m_in;
m_in[1]= "1111111111111111";
m_in[2]= "2222222222222222";
m_in[3]= "3333333333333333";
m_in[4]= "4444444444444444";
m_in[0]= "5555555555555555";
for(it_m_in = m_in.begin(); it_m_in!=m_in.end();)
{
cout<<" "<<it_m_in->first<<" --> "<<it_m_in->second<<"\n";
m_in.erase(it_m_in++);
}
cout<<endl;
return 0;
}
清除嵌套map
#include<map>
#include<string>
#include<iostream>
using namespace std;
int main()
{
map<int, string> m_in;
map<int,string>::iterator it_m_in;
map<int, map<int, string>> m;
map<int, map<int, string>>::iterator it_m;
m_in[1]= "1111111111111111";
m_in[2]= "2222222222222222";
m_in[3]= "3333333333333333";
m_in[4]= "4444444444444444";
m_in[0]= "5555555555555555";
m[11] = m_in;
for(it_m=m.begin();it_m!=m.end();)
{
cout<<"key: "<<it_m->first <<"\n value: \n";
for(it_m_in = m_in.begin(); it_m_in!=m_in.end();)
{
cout<<" "<<it_m_in->first<<" --> "<<it_m_in->second<<"\n";
m_in.erase(it_m_in++);
}
cout<<endl;
m.erase(it_m++);
}
return 0;
}
总结
嵌套map要一步一步删除。