diff --git a/ch09/README.md b/ch09/README.md index 39a667d6..e5cb5d3d 100644 --- a/ch09/README.md +++ b/ch09/README.md @@ -235,7 +235,7 @@ void insert(forward_list &flst, string find, string insrt) vec.resize(100) do? What if we next wrote vec.resize(10)? ```cpp -vec.resize(100); // adds 75 elements of value 0 to the back of vec +vec.resize(100); // adds 75 items to the back of vec. These added items are value initialized. vec.resize(10); // erases 90 elements from the back of vec ``` diff --git a/ch09/ex9_33.cpp b/ch09/ex9_33.cpp index 60735153..5f474663 100644 --- a/ch09/ex9_33.cpp +++ b/ch09/ex9_33.cpp @@ -8,7 +8,8 @@ // @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. +// @Answer crash, cause the iterator is invalid after insert. Because Iterators, pointers, and references to a +// vector or string are invalid if the container was reallocated. #include #include diff --git a/ch09/ex9_52.cpp b/ch09/ex9_52.cpp index bfab9c30..831ea1d8 100644 --- a/ch09/ex9_52.cpp +++ b/ch09/ex9_52.cpp @@ -1,15 +1,15 @@ // // ex9_52.cpp -// Exercise 9.52 +// Exercise 9.52 // // Created by pezy on 12/5/14. // Copyright (c) 2014 pezy. All rights reserved. // -// @Brief Use a stack to process parenthesized expressions. -// When you see an open parenthesis, note that it was seen. -// When you see a close parenthesis after an open parenthesis, -// pop elements down to and including the open parenthesis off the stack. -// push a value onto the stack to indicate that a parenthesized expression was replaced. +// @Brief Use a stack to process parenthesized expressions. +// When you see an open parenthesis, note that it was seen. +// When you see a close parenthesis after an open parenthesis, +// pop elements down to and including the open parenthesis off the stack. +// push a value onto the stack to indicate that a parenthesized expression was replaced. #include using std::stack; @@ -22,12 +22,13 @@ using std::cout; using std::endl; int main() { - string expr = "This is (Mooophy(awesome)((((wooooooooo))))) and (ocxs) over"; - char repl = '#'; + auto & expr = "This is (Mooophy(awesome)((((wooooooooo))))) and (ocxs) over"; + auto repl = '#'; + auto seen = 0; + stack stk; - int seen = 0; - for ( auto c : expr ) { + for (auto c : expr) { stk.push(c); if ( c == '(' ) ++seen; // open if ( seen && c == ')' ) { // pop elements down to the stack diff --git a/ch10/ex10_07.cpp b/ch10/ex10_07.cpp index 939ceb59..57403d3d 100644 --- a/ch10/ex10_07.cpp +++ b/ch10/ex10_07.cpp @@ -36,6 +36,9 @@ int main() // the destination is large enough to hold the number of elements being written. copy(lst.cbegin(), lst.cend(), vec.begin()); + //another way to fix bug + //copy(lst.cbegin(), lst.cend(), back_inserter(vec)); + // (b) vector v; v.reserve(10);