<small id='smTCy'></small><noframes id='smTCy'>

    1. <i id='smTCy'><tr id='smTCy'><dt id='smTCy'><q id='smTCy'><span id='smTCy'><b id='smTCy'><form id='smTCy'><ins id='smTCy'></ins><ul id='smTCy'></ul><sub id='smTCy'></sub></form><legend id='smTCy'></legend><bdo id='smTCy'><pre id='smTCy'><center id='smTCy'></center></pre></bdo></b><th id='smTCy'></th></span></q></dt></tr></i><div id='smTCy'><tfoot id='smTCy'></tfoot><dl id='smTCy'><fieldset id='smTCy'></fieldset></dl></div>
    2. <tfoot id='smTCy'></tfoot>
      • <bdo id='smTCy'></bdo><ul id='smTCy'></ul>
      <legend id='smTCy'><style id='smTCy'><dir id='smTCy'><q id='smTCy'></q></dir></style></legend>
    3. 为什么我不能使用“中断"?C++ 中三元条件语句中的语句?

      Why can#39;t I use a quot;breakquot; statement inside a ternary conditional statement in C++?(为什么我不能使用“中断?C++ 中三元条件语句中的语句?)
    4. <small id='1gUMj'></small><noframes id='1gUMj'>

      • <i id='1gUMj'><tr id='1gUMj'><dt id='1gUMj'><q id='1gUMj'><span id='1gUMj'><b id='1gUMj'><form id='1gUMj'><ins id='1gUMj'></ins><ul id='1gUMj'></ul><sub id='1gUMj'></sub></form><legend id='1gUMj'></legend><bdo id='1gUMj'><pre id='1gUMj'><center id='1gUMj'></center></pre></bdo></b><th id='1gUMj'></th></span></q></dt></tr></i><div id='1gUMj'><tfoot id='1gUMj'></tfoot><dl id='1gUMj'><fieldset id='1gUMj'></fieldset></dl></div>
          <tfoot id='1gUMj'></tfoot><legend id='1gUMj'><style id='1gUMj'><dir id='1gUMj'><q id='1gUMj'></q></dir></style></legend>
            <bdo id='1gUMj'></bdo><ul id='1gUMj'></ul>

                  <tbody id='1gUMj'></tbody>

                本文介绍了为什么我不能使用“中断"?C++ 中三元条件语句中的语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                Node 是一个非常简单的类,只有一个构造函数和几个变量:一个名称"(实际上只是一个字符)和两个称为左"和右"的子节点指针.

                Node is a very simple class with a just a constructor and a few variables: a "name" (actually just a char) and two child Node pointers called "left" and "right".

                我刚开始写一些需要放到最左边节点的代码,当我想到这个时我很高兴:

                I was just starting to write some code that needs to drop to the left-most node, and I was quite pleased when I came up with this:

                Node *current = this->root;
                while (true) (current->left != nullptr) ? current = current->left : break;
                

                看起来很简单:在一个无限循环中,检查 current 是否有左孩子,如果有,则将 current 设置为那个左孩子,如果没有,则跳出循环.这是一个很酷的小单线,不太难读.(我评论了!)

                Seems simple enough: in an infinite loop, check to see if current has a left child, if so, set current to that left child, if not, break out of the loop. It's a cool little one-liner, not too unreadable. (And I commented it!)

                好吧,我的编译器不喜欢它:

                Well, my compiler doesn't like it:

                iterator.cpp:20:70: error: expected expression
                    while (true) (current->left != nullptr) ? current = current->left : break;
                                                                                        ^
                1 error generated.
                

                此外,仅在 while 循环中加入一些大括号并将三元运算符移动到它自己的行也无济于事(不出所料).我不得不把它变成一个 if/else 以便编译器接受它.

                Also, just throwing some braces into the while loop and moving the ternary operator to it's own line didn't help (unsurprisingly). I had to turn it into an if/else for the compiler to accept it.

                有人能解释一下它是如何解释单行的以及它为什么反对吗?

                Can someone explain how it's interpreting the one-liner and why it objects?

                推荐答案

                三元条件运算符是一种将多个表达式组合成一个更大表达式的运算符.break 是一个语句而不是一个表达式,所以它不能在三元条件表达式中使用.

                The ternary conditional operator is an operator that combines multiple expressions into a larger expression. break is a statement and not an expression, so it can't be used inside a ternary conditional expression.

                不过,您可以像这样重写代码:

                You could, though, rewrite your code like this:

                while (current->left != nullptr) current = current->left;
                

                希望这有帮助!

                这篇关于为什么我不能使用“中断"?C++ 中三元条件语句中的语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Consistent pseudo-random numbers across platforms(跨平台一致的伪随机数)
                Vary range of uniform_int_distribution(改变uniform_int_distribution的范围)
                What is a seed in terms of generating a random number?(就生成随机数而言,种子是什么?)
                Is 1.0 a valid output from std::generate_canonical?(1.0 是 std::generate_canonical 的有效输出吗?)
                Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
                What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)

                1. <i id='iy2J8'><tr id='iy2J8'><dt id='iy2J8'><q id='iy2J8'><span id='iy2J8'><b id='iy2J8'><form id='iy2J8'><ins id='iy2J8'></ins><ul id='iy2J8'></ul><sub id='iy2J8'></sub></form><legend id='iy2J8'></legend><bdo id='iy2J8'><pre id='iy2J8'><center id='iy2J8'></center></pre></bdo></b><th id='iy2J8'></th></span></q></dt></tr></i><div id='iy2J8'><tfoot id='iy2J8'></tfoot><dl id='iy2J8'><fieldset id='iy2J8'></fieldset></dl></div>

                    • <bdo id='iy2J8'></bdo><ul id='iy2J8'></ul>

                      <small id='iy2J8'></small><noframes id='iy2J8'>

                      <tfoot id='iy2J8'></tfoot>
                      • <legend id='iy2J8'><style id='iy2J8'><dir id='iy2J8'><q id='iy2J8'></q></dir></style></legend>

                            <tbody id='iy2J8'></tbody>