LearnFrenzy
Sign in Take a free test

Arrays and Vectors · Predict the Output · Question 8

std::erase and std::erase_if in C++20 Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 8 Back to Exercise

What is the output of the following C++ program on a 64-bit system?

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

int main() {
    std::vector<int> v{2, 7, 2, 9, 4, 2, 11};
    auto n = std::erase(v, 2);
    auto m = std::erase_if(v, [](int x) { return x % 2 == 1; });
    std::cout << n << ' ' << m << ' ' << v.size() << ' ' << v[0] << '\n';
    return 0;
}
  • A 3 3 1 4 Correct
  • B 1 3 3 7
  • C 3 2 2 4
  • D 3 3 1 2
Correct Answer: Option A

Three 2s are removed, then the odd values 7, 9 and 11, leaving only 4. Both functions return how many elements they removed.

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