Skip to content

Commit

Permalink
fixed e9.28
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Apr 24, 2017
1 parent 2f00ac2 commit c024b35
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
17 changes: 7 additions & 10 deletions ch09/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> 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<string>` 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<string> &list, string const& to_find, string const& to_add)
void find_and_insert(forward_list<string> &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)?
Expand Down
45 changes: 45 additions & 0 deletions ch09/ex9_28_TEST.cpp
Original file line number Diff line number Diff line change
@@ -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 <forward_list>
#include <string>

#define CATCH_CONFIG_MAIN
#include "catch.hpp"

using std::forward_list;
using std::string;

void find_and_insert(forward_list<string> &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<string> l{ "a", "b", "c", "d", "e", "f" };
forward_list<string> 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<string> l{ "a", "b", "c", "d", "e", "f" };
forward_list<string> 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<string> l{ "a", "b", "c", "d", "b", "f" };
forward_list<string> r{ "a", "b", "g", "c", "d", "b", "g", "f" };
find_and_insert(l, "b", "g");
REQUIRE(l == r);
}

0 comments on commit c024b35

Please sign in to comment.