map-소스 한개
Posted 2012. 8. 13. 22:18#include <iostream>
#include <cstring>
#include <map>
using namespace std;
typedef struct PersonInfo
{
char Name[20];
int Age;
char ID[20];
char PW[20];
}Person;
map<int, Person*> Data;
map<int, Person*>::iterator iter;
void MainMenu();
int SelectMenu();
int InputNumber();
void InputData(int num);
void DataShowAll();
void DataShowSelect();
void DataDeleteSelect();
void DataDeleteAll();
void main ()
{
int Select;
int InputNum;
while(1)
{
MainMenu();
Select = SelectMenu();
switch(Select)
{
case 1://입력
InputNum = InputNumber();
InputData(InputNum);
break;
case 2://전체출력
DataShowAll();
break;
case 3://선택출력
DataShowSelect();
break;
case 4://선택삭제
DataDeleteSelect();
break;
case 5://전체삭제
DataDeleteAll();
break;
case 6:
cout<<"프로그램 종료"<<endl;
DataDeleteAll();
return;
break;
}
};
}
void MainMenu()
{
cout<<"----------"<<endl;
cout<<" map~ "<<endl;
cout<<"----------"<<endl;
cout<<"1.입 력"<<endl;
cout<<"2.전체출력"<<endl;
cout<<"3.선택출력"<<endl;
cout<<"4.선택삭제"<<endl;
cout<<"5.전체삭제"<<endl;
cout<<"6.종 료"<<endl;
cout<<"----------"<<endl;
}
int SelectMenu()
{
int Select;
cout<<"선 택 : ";
cin>>Select;
return Select;
}
int InputNumber()
{
int num;
cout<<"몇명 입력 : ";
cin>>num;
return num;
}
void InputData(int num)
{
for(int i=0 ; i<num ; i++)
{
Person* ptmp = new Person;
cout<<"이 름 : ";
cin>>ptmp->Name;
cout<<"나 이 : ";
cin>>ptmp->Age;
cout<<"I D : ";
cin>>ptmp->ID;
cout<<"P W : ";
cin>>ptmp->PW;
Data[i] = ptmp; //배열처럼 첫번째 키값에 second로 tmp를 넣음. 이게 곧 push_back()
}
}
void DataShowAll()
{
if( Data.empty() )
{
cout<<"입력된 정보가 없쑴"<<endl;
return;
}
for(iter = Data.begin() ; iter != Data.end() ; iter++) //
{
cout<<"이 름 : "<<(*iter).second->Name<<endl;
cout<<"나 이 : "<<(*iter).second->Age<<endl;
cout<<"I D : "<<(*iter).second->ID<<endl;
cout<<"P W : "<<(*iter).second->PW<<endl;
}
cout<<"출력 완료"<<endl;
}
void DataShowSelect()
{
if( Data.empty() )
{
cout<<"입력된 정보 없쑴"<<endl;
return;
}
int num;
cout<<"검색할 정보 입력"<<endl;
cout<<"1. 이 름, 2. I D"<<endl;
cout<<"선 택 : ";
cin>>num;
char tmp[20];
if(num == 1)
{
cout<<"이름 입력 : ";
cin>>tmp;
for(iter = Data.begin() ; iter != Data.end() ; )
{
if( !strcmp(tmp, (*iter).second->Name) )
{
cout<<"이 름 : "<<(*iter).second->Name<<endl;
cout<<"나 이 : "<<(*iter).second->Age<<endl;
cout<<"I D : "<<(*iter).second->ID<<endl;
cout<<"P W : "<<(*iter).second->PW<<endl;
cout<<"출력 완료"<<endl;
return;
}
else
iter++;
}
}
else if(num == 2)
{
cout<<"I D 입력 : ";
cin>>tmp;
for(iter = Data.begin() ; iter != Data.end() ; )
{
if( !strcmp(tmp, (*iter).second->ID) )
{
cout<<"이 름 : "<<(*iter).second->Name<<endl;
cout<<"나 이 : "<<(*iter).second->Age<<endl;
cout<<"I D : "<<(*iter).second->ID<<endl;
cout<<"P W : "<<(*iter).second->PW<<endl;
cout<<"출력 완료"<<endl;
return;
}
else
iter++;
}
}
}
void DataDeleteSelect()
{
if( Data.empty() )
{
cout<<"입력된 정보 없쑴"<<endl;
return;
}
char tmp[20];
int num;
cout<<"삭제할 정보 검색"<<endl;
cout<<"1. 이 름, 2. I D"<<endl;
cout<<"선 택 : ";
cin>>num;
if(num == 1)
{
cout<<"이름 입력 : ";
cin>>tmp;
for(iter = Data.begin() ; iter != Data.end() ; )
{
if( !strcmp(tmp, (*iter).second->Name) )
{
delete (*iter).second; //vector와 list의 차이점은 second에 잡혀있는 메모리를 해제하는것
iter = Data.erase(iter);
cout<<"삭제완료"<<endl;
return;
}
else
iter++;
}
}
else if(num == 2)
{
cout<<"I D 입력 : ";
cin>>tmp;
for(iter = Data.begin() ; iter != Data.end() ; )
{
if( !strcmp(tmp, (*iter).second->ID) )
{
delete (*iter).second; //vector와 list의 차이점은 second에 잡혀있는 메모리를 해제하는것
iter = Data.erase(iter);
cout<<"삭제완료"<<endl;
return;
}
else
iter++;
}
}
cout<<"삭제 완료"<<endl;
}
void DataDeleteAll()
{
if( Data.empty() )
{
cout<<"입력된 정보 없쑴"<<endl;
return;
}
for(iter = Data.begin() ; iter != Data.end() ; iter++)
{
delete (*iter).second; //vector와 list의 차이점은 second에 잡혀있는 메모리를 해제하는것
}
Data.clear(); //그다음 clear로 객체 비움
cout<<"전체 삭제 완료"<<endl;
}
- Filed under : C++/STL-map