Q:

In the final example in this section what would happen if we did not assign the result of insert to begin? Write a program that omits this assignment to see if your expectation was correct

0

In the final example in this section what would happen if we did not assign the result of insert to begin? Write a program that omits this assignment to see if your expectation was correct.

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> v{1, 2, 3, 4, 5};

  auto begin = v.begin();
  while (begin != v.end()) {
    ++begin;
    begin = v.insert(begin, 42);
    //v.insert(begin, 42);
    // Iterator `begin` will become invalid after insert, thus if we don't
    // update the iterator, any usage of this iterator will have undefined
    // behaviours.
    ++begin;
  }

  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