LearnFrenzy
Sign in Take a free test

STL Algorithms and Iterators · Find the Error · Question 5

Error: a predicate with the wrong number of parameters Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 5 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v{3, 8, 12, 5};
    auto n = std::count_if(v.begin(), v.end(), [](int a, int b) { return a > b; });
    std::cout << n << '\n';
    return 0;
}
  • A count_if returns bool
  • B count_if calls its predicate with ONE element; a two-parameter lambda (a comparator) cannot be called that way Correct
  • C v must be sorted
  • D The lambda must return int
Correct Answer: Option B

Write [](int x) { return x > 6; } for a unary predicate.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links