STL基础3:map

时间:2021-06-20 21:59:20
#include <iostream>
#include <map>
#include <string> using namespace std; #define N_K 10 typedef struct {
int a;
char b[]; } Data_t; void makeData(Data_t *d)
{
int i;
for (i=;i<N_K;i++)
{
d[i].a = i;
sprintf(d[i].b,"b=%02d",i);
}
} int main()
{
map<int,Data_t> m;
Data_t d[N_K];
Data_t *p;
pair<map<int, Data_t>::iterator, bool> insertPair; makeData(d);
for (int i=;i<N_K;i++)
{
m.insert(pair<int, Data_t>(i, d[i]));
} //验证插入,map不允许重复
insertPair = m.insert(pair<int, Data_t>(, d[]));
if(insertPair.second == true)
cout<<"Insert Successfully"<<endl;
else
cout<<"Insert Failure"<<endl; //遍历
map<int,Data_t>::iterator iter;
for(iter=m.begin(); iter!=m.end(); iter++){
cout<<iter->first<<" "<<(iter->second).b<<endl; }
//查找 修改 并删除
iter = m.find();
if(iter != m.end()){
cout<<"find 3 modify delete "<<iter->first<<" "<<(iter->second).b<<endl;
(iter->second).a = ;//map是深拷贝,所以在这里并没有修改d的数据
m.erase(iter);
} m.clear();
return ; }