vector-소스 한개

Posted 2012. 8. 13. 22:20

#include <vector>

#include <iostream>

 #include <cstring>

using namespace std;

 

void MainMenu();

int SearchData();

 

typedef struct PersonInfo{
 char Name[20];
 int Age;
 char ID[20];
 char PW[20];
}Person;

 

void main()

{

 vector<Person> Data;

// iter를 선언해도 되지만 배열은 바로 접근ㅇ ㅣ가능하기에 그냥 사용 

 while(1)

{

MainMenu();
cin >> Select;

if(Select == 1)

{

Person temp;
cout << "이  름 : ";
cin >> temp.Name;
cout << "나  이 : ";
cin >> temp.Age;
cout << "I    D : ";
cin >> temp.ID;
cout << "P    W : ";
cin >> temp.PW;
Data.push_back(temp);

}
  else if(Select == 2){
   for(int i = 0 ; i < Data.size() ; i++){
    cout << "이 름 : " << Data[i].Name << endl;
    cout << "나 이 : " << Data[i].Age << endl;
    cout << "I   D : " << Data[i].ID << endl;
    cout << "P   W : " << Data[i].PW << endl;
   }
  }
  else if(Select == 3){
   if(SearchData() == 1){
    char name[20];
    cout << "이름 입력 : ";
    cin >> name;
    for(int i = 0 ; i < Data.size() ; i++){
     if(!strcmp(name, Data[i].Name))
      Data.erase(Data.begin() + i);
    }
   }
   else{
    char id[20];
    cout << "I  D 입력 : ";
    cin >> id;
    for(int i = 0 ; i < Data.size() ; i++){
     if(!strcmp(id, Data[i].ID))
      Data.erase(Data.begin() + i);
    }
   }
  }
  else if(Select == 4){
   if(!Data.empty())
    Data.clear(); // Data.erase(Data.begin(), Data.end());
   else
    cout << "데이터가 없습니다" << endl;
  }
  else if(Select == 5) break;
 }

 

}

 

void MainMenu()

{

 cout << "--------------" <<endl;
 cout << "     vector     " << endl;
 cout << "--------------" <<endl;
 cout << "1.입      력" << endl;
 cout << "2.출      력" << endl;
 cout << "3.검색 삭제" << endl;
 cout << "4.전체 삭제" << endl;
 cout << "5.종     료" << endl;
 cout << "--------------" <<endl;
 cout << "선     택 : ";

}

 

 int SearchData()

{

 int i;
 cout << "검색할 목록 선택" << endl;
 cout << "1.이 름  2.I  D" << endl;
 cout << "선택 : ";
 cin >> i;

 return i;

}

'C++ > STL-vector' 카테고리의 다른 글

vector  (0) 2012.08.13