Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Challenge: Safe Vector Append | Exception Safety and Resource Management
C++ Exception Handling

bookChallenge: 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 dest is not modified at all;
  • Use only standard C++ and the libraries provided in this course.

Рішення

solution.cpp

solution.cpp

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 3
single

single

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

Can you show me an example of how to use this function?

What are some common pitfalls to avoid when ensuring strong exception safety in this scenario?

Can you explain what strong exception safety means in more detail?

close

Awesome!

Completion rate improved to 6.67

bookChallenge: 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 dest is not modified at all;
  • Use only standard C++ and the libraries provided in this course.

Рішення

solution.cpp

solution.cpp

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 3
single

single

some-alt