用于C ++的Sentinel while循环

时间:2010-02-01 12:53:39

标签: c++ loops while-loop

任何人都可以告诉我在C ++循环中什么是哨兵?请给我一个使用Sentinel while循环的例子。

6 个答案:

答案 0 :(得分:23)

此上下文中的“sentinel”是用于指示序列结束的特殊值。最常见的哨兵是字符串末尾的\ 0。 “endinel while loop”通常具有以下形式:

while (Get(input) != Sentinel) {
  Process(input);
}

答案 1 :(得分:2)

哨兵是特殊值,例如布尔值,非常大或小。它用于确定何时停止循环。

一个很好的例子是合并排序的实现,例如阅读http://www.cs.princeton.edu/courses/archive/spr07/cos226/lectures/04MergeQuick.pdf的第4页。

答案 2 :(得分:1)

作为JRL答案的附录..

请注意,提出这个问题并没有错,但是将来你可以通过访问dictionary.com并查找你不知道的单词来获得更多的直接帮助。

编辑:在这种情况下,字典不会让你思考。这是定义3:)

3 Also called  tag.  Computers. a symbol, mark, or other labeling device indicating the beginning or end of a unit of information.

答案 3 :(得分:0)

它通常是一个布尔值(true或false)变量,当条件不满足时设置为false,当它满足时设置为true。只要哨兵是假的,我们就可以循环。

示例:

bool acceptedAnswer = false;
while (acceptedAnswer == false)
{
  refreshPage();
  if (hasBeenAccepted())
  {
     acceptedAnswer = true;
  }
}

答案 4 :(得分:0)

sentinal是项目列表中的特殊值,它将始终导致迭代器停止。

例如,ASCIIZ字符串中的零终止符充当发送者。

链接列表通常有NULL指针,所以。

答案 5 :(得分:0)

while (score != -1)
{
    total += score;
    game ++;

    cout << "Enter the point for game " << game << ":";
    cin >> score;

    
}

cout << "The total score are: " << total << endl;