12.06.2017

#include <iostream>
#include <string>
#include <list>
using namespace std;

struct record{
string name;
int score;
};

void read_data(list<record> &a){ //call by reference, &안하면 그 안에 복사가 되지 않음.
record x;
for(int i = 0; i < 10; i++){
cout << "Enter name and score: ";
cin >> x.name >> x.score; //안하면 쓰레기값 10번 넣어주는 거임
a.push_back(x);
}

/* 10이 아니라 size로 돌릴 때
while(true){
cout << "Enter name and score: ";
cin >> x.name;
if(x.name == "quit") break;
cin >> x.score;
a.push_back(x);
}*/
}

void find(list<record> &a){
string name;
cout << "Enter name to find: ";
cin >> name;

for(int i = 0; i < a.size(); i++){
if(a[i].name == name){
cout << a[i].score << endl;
return;
}
} cout << "찾을 수 없습니다." << endl;

/*iterator로 하기

list<record>::iterator it;

for(it = a.begin(); it != a.end(){
if(it->name == name){
        cout << a[i].score << endl;
return;
}
} cout << "찾을 수 없습니다. " <<endl;
*/
}

int main(){
list<record> a;

read_data(a);
find(a);

system("pause");
return 0;
}

댓글 없음:

댓글 쓰기