Q:

Assuming vi is a container of ints that includes even and odd values, predict the behavior of the following loop

0

Assuming vi is a container of ints that includes even and odd values, predict the behavior of the following loop. After you’ve analyzed this loop, write a program to test whether your expectations were correct.

iter = vi.begin();
while (iter != vi.end())
 if (*iter % 2) iter = vi.insert(iter, *iter); ++i

All Answers

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

#include <vector>

int main() {
  //std::vector<int> vi{1, 2, 3, 4};
  std::vector<int> vi{2, 4};

  // If the container contains any odd values, the loop will continue
  // infinitely.
  auto iter = vi.begin();
  while (iter != vi.end()) { // The loop body should be a block.
    if (*iter % 2)
      iter = vi.insert(iter, *iter);
    ++iter;
  }

  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