diff --git a/ch09/README.md b/ch09/README.md index aee7f9e4..14df0859 100644 --- a/ch09/README.md +++ b/ch09/README.md @@ -206,24 +206,21 @@ if both elem1 and elem2 are the off-the-end iterator, nothing happened too. ## [Exercise 9.27](ex9_27.cpp) ## Exercise 9.28: ->Write a function that takes a forward_list and two additional string arguments. The function should find the first string and insert the second immediately following the first. If the first string is not found, then insert the second string at the end of the list. +>Write a function that takes a `forward_list` and two additional string arguments. The function should find the first string and insert the second immediately following the first. If the first string is not found, then insert the second string at the end of the list. ```cpp -void find_and_insert(forward_list &list, string const& to_find, string const& to_add) +void find_and_insert(forward_list &list, const string& to_find, const string& to_add) { auto prev = list.before_begin(); + auto size = std::distance(list.begin(), list.end()); for (auto curr = list.begin(); curr != list.end(); prev = curr++) - { - if (*curr == to_find) - { - list.insert_after(curr, to_add); - return; - } - } - list.insert_after(prev, to_add); + if (*curr == to_find) list.insert_after(curr, to_add); + if (size == std::distance(list.begin(), list.end())) list.insert_after(prev, to_add); } ``` +[UnitTest](ex9_28_TEST.cpp) + ## Exercise 9.29: >Given that vec holds 25 elements, what does vec.resize(100) do? What if we next wrote vec.resize(10)? diff --git a/ch09/ex9_28_TEST.cpp b/ch09/ex9_28_TEST.cpp new file mode 100644 index 00000000..c505220c --- /dev/null +++ b/ch09/ex9_28_TEST.cpp @@ -0,0 +1,45 @@ +//! @file Exercise 9.28 +//! @author pezy +//! @date 2017-04-24 +//! @Brief Write a function that takes a forward_list and two additional string arguments. +//! The function should find the first string and insert the second immediately following the first. +//! If the first string is not found, then insert the second string at the end of the list. + +#include +#include + +#define CATCH_CONFIG_MAIN +#include "catch.hpp" + +using std::forward_list; +using std::string; + +void find_and_insert(forward_list &list, const string& to_find, const string& to_add) +{ + auto prev = list.before_begin(); + auto size = std::distance(list.begin(), list.end()); + for (auto curr = list.begin(); curr != list.end(); prev = curr++) + if (*curr == to_find) list.insert_after(curr, to_add); + if (size == std::distance(list.begin(), list.end())) list.insert_after(prev, to_add); +} + +TEST_CASE("common", "[find_and_insert]") { + forward_list l{ "a", "b", "c", "d", "e", "f" }; + forward_list r{ "a", "b", "c", "g", "d", "e", "f" }; + find_and_insert(l, "c", "g"); + REQUIRE(l == r); +} + +TEST_CASE("not found", "[find_and_insert]") { + forward_list l{ "a", "b", "c", "d", "e", "f" }; + forward_list r{ "a", "b", "c", "d", "e", "f", "g" }; + find_and_insert(l, "g", "g"); + REQUIRE(l == r); +} + +TEST_CASE("multi found", "[find_and_insert]") { + forward_list l{ "a", "b", "c", "d", "b", "f" }; + forward_list r{ "a", "b", "g", "c", "d", "b", "g", "f" }; + find_and_insert(l, "b", "g"); + REQUIRE(l == r); +} \ No newline at end of file