Skip to content

Commit

Permalink
fixed Mooophy#136
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Feb 2, 2015
1 parent c632012 commit b5b2de5
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions ch09/ex9_34.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,37 @@
// @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.
//
// iter = vi.begin();
// while (iter != vi.end())
// if (*iter % 2)
// iter = vi.insert(iter, *iter);
// ++iter;
//
// @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::cout; using std::endl;

using std::vector; using std::cout; using std::endl;
#include <vector>
using std::vector;

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;
}
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;
}
++iter;
}

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

return 0;
}

0 comments on commit b5b2de5

Please sign in to comment.