<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++ 中三元条件语句中的语句?

      时间:2023-06-04
    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++ 中三元条件语句中的语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:C/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>
                  1. <legend id='iy2J8'><style id='iy2J8'><dir id='iy2J8'><q id='iy2J8'></q></dir></style></legend>