Skip to content

Commit

Permalink
refuctor ex9_31_32_34.cpp and added 9.33
Browse files Browse the repository at this point in the history
1. 9.31 have an other case: forward_list
2. 9.32 it's not always illegal.
3. 9.34 infinite loop added fixed solution.
  • Loading branch information
pezy committed Dec 3, 2014
1 parent 2621431 commit 2d823e3
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 114 deletions.
5 changes: 5 additions & 0 deletions ch09/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,8 @@ that takes a single argument place on the element type?

If the container holds elements of a class type and resize adds elements
we **must supply an initializer** or the element type must have a **default constructor**.

## Exercise 9.31 [use list](ex9_31_1.cpp) | [use forward_list](ex9_31_2.cpp)
## [Exercise 9.32](ex9_32.cpp)
## [Exercise 9.33](ex9_33.cpp)
## [Exercise 9.34](ex9_34.cpp)
37 changes: 37 additions & 0 deletions ch09/ex9_31_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// ex9_31.cpp
// Exercise 9.31
//
// Created by pezy on 12/3/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// @Brief The program on page 354 to remove even-valued elements and
// duplicate odd ones will not work on a list or forward_list. Why?
// Revise the program so that it works on these types as well.
// @list iter += 2; -> advance(iter, 2);
//

#include <iostream>
#include <list>

using std::list; using std::cout; using std::endl; using std::advance;

int main()
{
list<int> vi = {0,1,2,3,4,5,6,7,8,9};
auto iter = vi.begin();
while (iter != vi.end()) {
if (*iter % 2) {
iter = vi.insert(iter, *iter);
advance(iter, 2);
} else
iter = vi.erase(iter);
}

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

return 0;
}


40 changes: 40 additions & 0 deletions ch09/ex9_31_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// ex9_31.cpp
// Exercise 9.31
//
// Created by pezy on 12/3/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// @Brief The program on page 354 to remove even-valued elements and
// duplicate odd ones will not work on a list or forward_list. Why?
// Revise the program so that it works on these types as well.
// @forward_list 1. vi.insert -> vi.insert_after
// 2. iter += 2; -> advance(iter, 2);
// 3. vi.erase -> vi.erase_after
// 4. added iterator prev, and edit some detail

#include <iostream>
#include <forward_list>

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

int main()
{
forward_list<int> vi = {0,1,2,3,4,5,6,7,8,9};
auto iter = vi.begin(), prev = vi.before_begin();
while (iter != vi.end()) {
if (*iter % 2) {
iter = vi.insert_after(prev, *iter);
advance(iter, 2);
advance(prev, 2);
} else
iter = vi.erase_after(prev);
}

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

return 0;
}


114 changes: 0 additions & 114 deletions ch09/ex9_31_32_34.cpp

This file was deleted.

38 changes: 38 additions & 0 deletions ch09/ex9_32.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// ex9_32.cpp
// Exercise 9.32
//
// Created by pezy on 12/3/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// @Brief In the program onpage 354 would it be legal to write the call to insert as follows?
// If not, why not?
// iter = vi.insert(iter, *iter++);
// @Answer - If only changed this, it's illegal obviously. like @Mooophy said:
// "*iter++ can result in deferencing an item out of range causing a runtime error."
// - If change "iter += 2;" to "++iter;" after the call. It is legal.(follow codes)

#include <iostream>
#include <vector>

using std::vector; using std::cout; using std::endl;

int main()
{
vector<int> vi = {0,1,2,3,4,5,6,7,8,9};
auto iter = vi.begin();
while (iter != vi.end()) {
if (*iter % 2) {
iter = vi.insert(iter, *iter++);
++iter;
} else
iter = vi.erase(iter);
}

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

return 0;
}


34 changes: 34 additions & 0 deletions ch09/ex9_33.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// ex9_33.cpp
// Exercise 9.33
//
// Created by pezy on 12/3/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// @Brief In the final example in this section what would happen
// if we did not assign the result of insert to begin?
// Write a program that omits this assignment to see if your expectation was correct.
// @Answer crash, cause the iterator is invalid after insert.

#include <iostream>
#include <vector>

using std::vector; using std::cout; using std::endl;

int main()
{
vector<int> v{1,2,3,4,5,6,7,8,9};
auto begin = v.begin();
while (begin != v.end()) {
++begin;
/*begin = */v.insert(begin, 42);
++begin;
}

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

return 0;
}


36 changes: 36 additions & 0 deletions ch09/ex9_34.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// ex9_32.cpp
// Exercise 9.32
//
// Created by pezy on 12/3/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// @Brief Assuming vi is a container of ints that includes even and odd values,
// predict the behavior of the following loop.
// After you've analyzed this loop, write a program to test whether your expectations were correct.
// @Answer "infinite loop". Casue the `++iter` out of the `while` loop. But even through add brackets it is still infinite loop.
// Fixed following.

#include <iostream>
#include <vector>

using std::vector; using std::cout; using std::endl;

int main()
{
vector<int> vi = {0,1,2,3,4,5,6,7,8,9};
auto iter = vi.begin();
while (iter != vi.end())
{
if (*iter % 2)
iter = vi.insert(iter, *iter++);
++iter;
}

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

return 0;
}


0 comments on commit 2d823e3

Please sign in to comment.