Challenge: Safe Vector Append
In this challenge, your goal is to write a function that appends elements from one std::vector<int> to another, while guaranteeing strong exception safety. If an exception occurs during the append processβsuch as a memory allocation failureβthe original destination vector should remain unchanged. This means you must ensure that either all elements are successfully appended, or the vector is left exactly as it was before the operation began.
Swipe to start coding
Write a function safe_vector_append that takes two references to std::vector<int>, called dest and src. The function should append all elements from src to dest. If an exception occurs during the append operation, dest must remain unchanged. Use only standard C++ and the libraries available for this course.
- Ensure that the append operation is strongly exception safe: either all elements are appended, or
destis not modified at all; - Use only standard C++ and the libraries provided in this course.
Solution
solution.cpp
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 6.67
Challenge: Safe Vector Append
Swipe to show menu
In this challenge, your goal is to write a function that appends elements from one std::vector<int> to another, while guaranteeing strong exception safety. If an exception occurs during the append processβsuch as a memory allocation failureβthe original destination vector should remain unchanged. This means you must ensure that either all elements are successfully appended, or the vector is left exactly as it was before the operation began.
Swipe to start coding
Write a function safe_vector_append that takes two references to std::vector<int>, called dest and src. The function should append all elements from src to dest. If an exception occurs during the append operation, dest must remain unchanged. Use only standard C++ and the libraries available for this course.
- Ensure that the append operation is strongly exception safe: either all elements are appended, or
destis not modified at all; - Use only standard C++ and the libraries provided in this course.
Solution
solution.cpp
Thanks for your feedback!
single