string s="< feature token = \"do\" id = \"98\" freq = \"1\" />";
string z = searchValue(s,'\"');
string y = searchValue(s,'\"');
string x = searchValue(s,'\"');
cout << x << endl << y << endl << z << endl;
What you're doing wrong is assuming that that functions in a << expression evaluate in left-to-right order. They don't necessarily. And in this case your compiler is evaluating them right to left.
(To clarify, since the original question didn't say, the behavior he's seeing is that it's printing out:
1
98
do
instead. (Or at least, that's what I assume because that's what I get when I compile it with g++)
What's happening is that the expressions on either side of an operator both have to be run before the operator, but they can be run in either order. For whatever reason, in this case, your compiler is running the rightmost call to searchValue first and then the middle one and then the left one. Since searchValue changes s, this, of course, changes the answer.
You might think that the code you wrote is guaranteed to give you the same answer as:
string s="< feature token = \"do\" id = \"98\" freq = \"1\" />";
string x = searchValue(s,'\"');
string y = searchValue(s,'\"');
string z = searchValue(s,'\"');
cout << x << endl << y << endl << z << endl;
but it isn't, and, at least in this particular case, it doesn't. If you run that code, you'll see that it gives you the expected result. Your code is running as if it were written like this:
string s="< feature token = \"do\" id = \"98\" freq = \"1\" />";
string z = searchValue(s,'\"');
string y = searchValue(s,'\"');
string x = searchValue(s,'\"');
cout << x << endl << y << endl << z << endl;
发布评论
评论(1)
你做错的事情是假设它在 << 中起作用表达式按从左到右的顺序求值。他们不一定。在这种情况下,您的编译器将从右到左评估它们。
(澄清一下,由于最初的问题没有说,他看到的行为是打印出来:
相反。(或者至少,这是我的假设,因为这是我用 g++ 编译它时得到的结果)
发生的事情是运算符两侧的表达式都必须在运算符之前运行,但无论出于何种原因,它们都可以按任意顺序运行,在这种情况下,编译器首先运行最右边的 searchValue 调用,然后运行中间的调用。然后是左边由于 searchValue 改变了 s,这当然会改变答案,
您可能认为您编写的代码一定会给出与以下相同的答案:
但事实并非如此,并且至少在这种特殊情况下,它是这样的。如果您运行该代码,您会看到它给出了预期的结果,就像这样编写的:
What you're doing wrong is assuming that that functions in a << expression evaluate in left-to-right order. They don't necessarily. And in this case your compiler is evaluating them right to left.
(To clarify, since the original question didn't say, the behavior he's seeing is that it's printing out:
instead. (Or at least, that's what I assume because that's what I get when I compile it with g++)
What's happening is that the expressions on either side of an operator both have to be run before the operator, but they can be run in either order. For whatever reason, in this case, your compiler is running the rightmost call to searchValue first and then the middle one and then the left one. Since searchValue changes s, this, of course, changes the answer.
You might think that the code you wrote is guaranteed to give you the same answer as:
but it isn't, and, at least in this particular case, it doesn't. If you run that code, you'll see that it gives you the expected result. Your code is running as if it were written like this: