我需要帮助理解括号的使用

时间:2017-05-11 08:09:47

标签: c++

为什么代码1在if语句之后没有括号而代码2不起作用。代码是通知重复相同的字符串。

代码1:

int main()
{
  string previous = " ";
  string current;
  while(cin >> current){
    if(previous == current)
    {
      cout << "repeated word: " << current << endl;
      previous = current;
    }
  }
}

代码2:

@myExtension

1 个答案:

答案 0 :(得分:6)

此代码:

if(previous == current)
{
  cout << "repeated word: " << current << endl;
}
previous = current;

等于:

if(previous == current)
{
  cout << "repeated word: " << current << endl;
  previous = current;
}

显然,等于:

previous = current;

您在第一个代码中放在public function addCommentAction($ediTransactionId, Request $request){ $em = $this->getDoctrine()->getManager(); $trans = $em->getRepository('MatrixEdiBundle:ediTransaction')->findOneBy($ediTransactionId); $comment = $request->get("comment"); // <-- I changed this line $trans->setComment($comment); // <-- I changed this line $em->persist($trans); $em->flush(); $session = $this->getRequest()->getSession(); $session->getFlashBag()->add('message', 'Your comment has been saved.'); return $this->redirect($this->generateUrl('matrix_edi_outputComment')); } 前面的标签或空格与编译器无关。这只是风格(在你的情况下是误导性的风格)。