Review this generated ledger import path.
Append a batch of non-negative amounts atomically, preserve failures for the caller, and manage temporary storage automatically.
C++
void append_amounts(
std::vector<int>& ledger,
const std::vector<int>& input) noexcept {
auto* scratch = new int[input.size()];
for (std::size_t i = 0; i < input.size(); ++i) {
ledger.push_back(input[i]);
if (input[i] < 0) {
throw std::invalid_argument("negative amount");
}
scratch[i] = input[i];
}
delete[] scratch;
}
void import(std::vector<int>& ledger, const std::vector<int>& input) {
try {
append_amounts(ledger, input);
} catch (std::exception error) {
std::cerr << error.what() << '\n';
}
}
generated code is illustrative, not from any one model