C ++有人可以解释这些for循环说的内容吗?

时间:2012-11-08 22:54:14

标签: c++ for-loop conditional-operator

所以这段代码是我复制过的在线boggle游戏的基本大纲。 消息来源:http://www.codingfriends.com/index.php/2010/06/10/boggle/

bool findUsersWord(string findThis, Grid<char> &theBoard, Vector<cell> &theRoute, string alreadyFound, int placeY, int placeX)
{  
  // need to find the findThis  base case
  if (findThis == alreadyFound)
    return true;
  // need to find the first letter within the board and then progress around that.
  if (alreadyFound.empty())
  {
    for (int rows = 0; rows < theBoard.numRows(); rows++)
      for (int cols = 0; cols < theBoard.numCols(); cols++)
        // find the each character within the 
        if (theBoard[rows][cols] == findThis[0])
        {
          alreadyFound = findThis[0];
          cell newR;
          newR.row = rows;
          newR.col = cols;
          theRoute.add(newR);
          if (findUsersWord(findThis, theBoard, theRoute, alreadyFound, rows, cols))
            return true;
          else
            // clear out the found Board 
            theRoute.clear();
        }
  }
  else
  {
    // try and find the next letters within the area around the base letter
    // spin around the letter 3 * 3 grid
    for (int y= (placeY > 0 ? placeY-1: placeY); y <=(placeY == (theBoard.numRows()-1) ? placeY : placeY+1);y++)
      for (int x=(placeX > 0 ? placeX-1: placeX); x<=(placeX == (theBoard.numCols()-1) ? placeX : placeX+1); x++)
        if ((theBoard[y][x] == findThis[alreadyFound.length()]) && (!(y==placeY && x ==placeX)))
          // already used letter
          if (!placeAlreadyUsed(y,x,theRoute))
          {
            alreadyFound += findThis[alreadyFound.length()];
            cell newR;
            newR.row = y;
            newR.col = x;
            theRoute.add(newR);
            if (findUsersWord(findThis, theBoard,theRoute, alreadyFound, y, x))
              return true;
            else
            {
              if (alreadyFound.length() > 1)
                alreadyFound = alreadyFound.substr(0, alreadyFound.length()-1);
              theRoute.removeAt(theRoute.size()-1);
            }
          }
    return false;
  }
  return false;
}

以下代码是有问题的代码,它是上述代码的一部分。

for (int y= (placeY > 0 ? placeY-1: placeY); y <=(placeY == (theBoard.numRows()-1) ? placeY : placeY+1);y++)
  for (int x=(placeX > 0 ? placeX-1: placeX); x<=(placeX == (theBoard.numCols()-1) ? placeX : placeX+1)

我想知道是否有人可以将此代码转换为更简单的代码,而不涉及使用?然后。我知道它的简单部分,如“?”表示返回,“:”表示下一行,但我迷失了它在for循环中使用的事实以及它看起来像

的事实
if(placeY > 0)
 return playceY-1
placeY;

我哪里出错?

2 个答案:

答案 0 :(得分:2)

? :块只是一个奇怪的if语句。如果你愿意,那就是内联

这是格式

argument ? result evaluated to if true : result evaluated to if false

这是一个例子

1<2 ? "Hurray" : "boo"

将评估为"Hurray",因为1&lt; 2为真。但是,如果我们将其切换为1>2,则会评估为"boo"

答案 1 :(得分:2)

  

我知道它的简单部分,如“?”表示返回,“:”表示下一行

嗯,不。这根本不是什么意思。 ?:是一个有三个操作数表达式的运算符,其中一个出现在?:之间。

placeY > 0 ? placeY-1 : placeY

表达式表示:“如果placeY > 0则评估placeY-1;否则评估placeY”。

代码的想法是,出于某种原因,我们希望迭代(placeX,placeY)旁边的所有电路板位置。这些位置形成一个矩形,?:运算符用于计算该矩形的左,右,上和下限。例如,上面引用的表达式用于顶部坐标。通常为placeY-1,但如果placeY已经为0,则其上方的板上没有行,在这种情况下placeY本身就是最上一行。