accumulate
- 배열의 합을 구하기 편한 STL
- numeric 헤더에 들어있다.
- T accumulate(InIt first, InIt last, T val [, BinOp op]);
- first ~ last 구간에 속한 값들의 총합을 구한다.
- 세번째 인자 val 은 누적의 총합의 초기값이며 0으로 지정했을 때 순수한 합을 구 할 수 있다.
...
vector<int> solution(vector<int> answers) {
vector<int> pattern_1 = { 1, 2, 3, 4, 5 };
vector<int> pattern_2 = { 2, 1, 2, 3, 2, 4, 2, 5 };
vector<int> pattern_3 = { 3, 3, 1, 1, 2, 2, 4, 4, 5, 5 };
vector<bool> info_1 = compCorrect(pattern_1, answers);
vector<bool> info_2 = compCorrect(pattern_2, answers);
vector<bool> info_3 = compCorrect(pattern_3, answers);
vector<int> result = { accumulate(info_1.begin(), info_1.end(), 0),
accumulate(info_2.begin(), info_2.end(), 0),
accumulate(info_3.begin(), info_3.end(), 0) };
vector<int> answer;
int max_num = *max_element(result.begin(), result.end());
for (int i = 0; i < result.size(); i++) {
if (result[i] == max_num) {
answer.push_back(i+1);
}
}
return answer;
}
- 수집한 정보 배열의 요소들(int들)을 합하기 위해 accumulate 를 사용했다.