The function recalculates prices in a catalog. The conversion can throw — bad input, overflow. After one such throw the catalog ended up half in old prices, half in new, and nobody could tell which half. Restarting does not help: the data is already inconsistent. The function must not touch the vector until it knows the whole conversion succeeds.
mapInPlace applies op to every element of the vector. If op throws midway, the vector is left half-transformed — first elements new, the rest old. Rewrite it with the all-or-nothing guarantee: either every element is transformed, or the vector stays exactly as it was. The exception must still reach the caller.
op(element), in orderop throws: the vector keeps its original content, element for elementDo not write into v at all while op can still throw. Collect the results somewhere else first.
Once every op(x) has succeeded, replacing the whole content of v in one step is cheap — vectors move.
Hit Submit (or ⌘/Ctrl + ↵) — test results will show up here.