forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refuctor ex9_31_32_34.cpp and added 9.33
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
Showing
7 changed files
with
190 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|