Skip to content

Commit

Permalink
added 9.27 and 9.28. deleted origin
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Dec 2, 2014
1 parent 989f50b commit 18743c0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 92 deletions.
18 changes: 18 additions & 0 deletions ch09/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,21 @@ if elem2 is the off-the-end iterator, it would delete from elem1 to the end.
if both elem1 and elem2 are the off-the-end iterator, nothing happened too.

## [Exercise 9.26](ex9_26.cpp)
## [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.
```cpp
void insert(forward_list<string> &flst, string find, string insrt)
{
auto prev = flst.before_begin();
for (auto curr = flst.begin(); curr != flst.end(); prev = curr++)
if (*curr == find)
{
flst.insert_after(curr, insrt);
return;
}
flst.insert_after(prev, insrt);
}
```
29 changes: 29 additions & 0 deletions ch09/ex9_27.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// ex9_27.cpp
// Exercise 9.27
//
// Created by pezy on 12/3/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// @Brief Write a program to find and remove the odd-valued
// elements in a forward_list<int>.

#include <iostream>
#include <forward_list>

using std::forward_list; using std::cout; using std::endl;

int main()
{
forward_list<int> flst = {0,1,2,3,4,5,6,7,8,9};
for (auto prev = flst.before_begin(), curr = flst.begin(); curr != flst.end(); )
if (*curr & 0x1) curr = flst.erase_after(prev);
else prev = curr++;

for (auto i : flst)
cout << i << " ";

return 0;
}


92 changes: 0 additions & 92 deletions ch09/ex9_27_28.cpp

This file was deleted.

0 comments on commit 18743c0

Please sign in to comment.