Make Unreal REAL.
article thumbnail

 

2의 개수

count(v.begin(), v.end(), 2);

 

30세 미만인 사람의 수

count_if(v.begin(), v.end(), [](const Person &prsn) { return prsn.age < 30; });

 

accumulate에서 다음은 같다.

accumulate(v.begin(), v.end(), 0);
// 전달된 Lambda 함수에서 n은 현재 까지의 합, e는 현재 원소를 뜻한다.
accumulate(v.begin(), v.end(), 0, [](const int n, const int e) { return n + e; });

 

2차원 벡터에서 8의 개수

  • vv의 원소 자료형이 vector<int>이기 때문에 Lambda 식 함수 op의 2번째 인자 형식도 맞춰주어야 한다.
// 현재까지의 합 n에 현재 행 v 내의 8의 개수를 더하여 계산한다.
accumulate(vv.begin(), vv.end(), 0, [](const int n, const vector<int>& v) { return n + count(v.begin(), v.end(), 8); });
profile

Make Unreal REAL.

@diesuki4

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

검색 태그