본문 바로가기
스터디/알고리즘

[ 프로그래머스 ] 완주하지 못한 선수

by 알 수 없는 사용자 2020. 2. 11.

사용 언어 : C++

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <string>
#include <vector>
 
using namespace std;
 
string solution(vector<string> participant, vector<string> completion) {
 
    vector<string>::iterator temp;
    string answer = "";
 
    for (auto it = participant.begin(); it != participant.end();)
    { 
        temp = find(completion.begin(), completion.end(), *it);
        if (temp != completion.end())
        {
            completion.erase(temp);
            it = participant.erase(it);
        }
        else
        {
            it++;
        }
    }  
 
    answer = *participant.begin();
 
    return answer;
 
}
 

 

에러

 

/solution0.cpp:13:16: error: use of undeclared identifier 'find'
temp = find(completion.begin(), completion.end(), *it);
^
1 error generated.
make[2]: *** [CMakeFiles/solution_test.dir/solution0.cpp.o] Error 1
make[1]: *** [CMakeFiles/solution_test.dir/all] Error 2
make: *** [all] Error 2

 

 

 

그래서 다음으로 변경

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <string>
#include <vector>
#include <algorithm>
 
using namespace std;
 
string solution(vector<string> participant, vector<string> completion) {
 
 
    for (int i = 0; i < participant.size(); ++i) {
        if (participant[i] != completion[i]) 
            return participant[i];
        if (i == participant.size() - 1)
            return participant[i];
    }
}