-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ch09/ex9_32.cpp #125
Comments
Hi~ @dom68 iter = vi.insert(iter, *iter++); Because as said in Standard [5.2.2]
Check this post on StackOverflow for more discussion. As for your version : iter = vi.insert(iter, *iter);
++iter; I tested it using the code from CP5, as this: #include <iostream>
#include <vector>
using namespace std;
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;
} I believe it's legal, but it led to an infinite loop. Please try it. |
MooophyCpp-Primer Date: Sat, 10 Jan 2015 19:25:29 -0800 Hi~ @dom68 Thx for issue reporting. The current answer in this repo indeed has problem. But it isn't like what you said. First of all, the following statement is illegal : iter = vi.insert(iter, *iter++); Because as said in Standard [5.2.2] The order of evaluation of arguments is unspecified. Check this post for more discussion. As a result, after entering function insert, iter could be its original value or original value + 1 or even anything else, depending on how compiler has been implemented. As for your version : iter = vi.insert(iter, *iter); I tested it using the code from CP5, as this: #include I believe it's legal, but it led to a infinite loop. Please run it. —
|
ch09/ex9_32.cpp
FYI, I believe in Update ex9_32.cpp
iter = vi.insert(iter, *iter++); statement
will insert *iter into the container after incrementing the pointer, not before the pre-incremented iter pointer.
tested it with the statement iter = vi.insert(iter, - *iter++); so I could discriminate. The corrected statement should be instead iter = vi.insert(iter, *iter); ++iter;
this statement may also refers to the section of the book 4.1.3 Order of Evaluation, Precedence, and Associativity where incrementing the second argiment will affect the first argument in the insert function
thanks,
dom68
The text was updated successfully, but these errors were encountered: