Q:

List three ways to define a vector and give it ten elements, each with the value 42. Indicate whether there is a preferred way to do so and why

0

List three ways to define a vector and give it ten elements, each with the value 42. Indicate whether there is a preferred way to do so and why.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <vector>
using std::vector;

int main() {
  vector<int> v1(10, 42);
  vector<int> v2{42, 42, 42, 42, 42, 42, 42, 42, 42, 42};
  vector<int> v3;
  for (int i = 0; i != 10; ++i)
    v3.push_back(42);

  // The way with parentheses is prefered here. It contains less code.

  return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now