Q:

Assuming iv is a vector of ints, what is wrong with the following program? How might you correct the problem(s)

0

Assuming iv is a vector of ints, what is wrong with the following program? How might you correct the problem(s)?

vector<int>::iterator iter = iv.begin(), mid = iv.begin() + iv.size()/2;
while (iter != mid)
 if (*iter == some_val) iv.insert(iter, 2 * some_val);

All Answers

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

    vector<int>::iterator iter = iv.begin(),
                          mid = iv.begin() + iv.size()/2;
    while (iter != mid) {
      if (*iter == some_val) {
        iv.insert(iter, 2 * some_val);  // Error, we should update the iter
        iter = iv.insert(iter, 2 * some_val);
        ++iter;  // Increament to point to the original value
      }
      ++iter;  // Increment to point to the next value
    }

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