有谁知道endl(cout)和cout<<之间的区别ENDL?

时间:2012-03-04 15:50:50

标签: c++ cout endl

我认为他们是同一回事,但是当我向在线法官发送代码时(endl(cout)),它给了我“错误的答案”判决,然后我试图用cout << endl发送另一个并且法官接受了代码!有谁知道这些命令之间的区别?

4 个答案:

答案 0 :(得分:3)

我不知道。

std::endl是一个获取流并返回流的函数:

ostream& endl ( ostream& os );

当您将其应用于std::cout时,它会立即应用该功能。

另一方面,std::basic_ostream的重载为operator<<并带有签名:

template <typename C, typename T>
basic_ostream<C,T>& operator<<(basic_ostream<C,T>& (*pf)(basic_ostream<C,T>&));

也将立即应用该功能。

因此,从技术上讲,即使流std::cout << std::endl更惯用,也没有区别。可能是法官机器人虽然过于简单,但却没有意识到这一点。

答案 1 :(得分:2)

唯一的区别是endl(cout)被视为全局函数,而在cout << endl中,endl被视为操纵符。但它们也有同样的效果。

答案 2 :(得分:1)

这两种形式之间的行为没有区别。两者都引用相同的endl函数,可以用作操纵器(cout << endl)或自由函数(endl(cout))。

答案 3 :(得分:1)

上面的答案是正确的!另外,根据您使用WITH recursion (employeeid, schoolyear, loc1, effectivedate, d, enddate, currentrecord, Level) AS ( select this.employeeid, this.schoolyear, this.loc1, this.effectivedate, this.effectivedate, this.enddate, this.currentrecord, 0 as level from Staging.EmployeeStaging2 as this left Join Staging.EmployeeStaging2 as [last] on DATEADD(day,1, [last].enddate) = this.effectivedate where ([last].employeeid=this.employeeid and [last].schoolyear=this.schoolyear) and [last].loc1 <> this.loc1 or [last].loc1 is NULL UNION ALL Select this.employeeid, this.schoolyear, this.loc1, this.effectivedate, last.d, this.enddate, this.currentrecord, level + 1 from Staging.EmployeeStaging2 as this inner join recursion as [last] on DATEADD(day,1, [last].enddate) = this.effectivedate where ([last].employeeid=this.employeeid and [last].schoolyear=this.schoolyear) and [last].loc1 = this.loc1 and level < 3500)select employeeid, schoolyear, loc1, d as start, max(enddate) as end1, currentrecord from recursion where schoolyear='1516' group by employeeid, schoolyear, d, loc1, currentrecord, level order by employeeid, schoolyear, d 还是<< endl;,它可以减少代码中的行数。

示例:

您可以拥有以下内容:

endl(cout)

OR

cout << "Hello World" << endl;

cout << "Hello World";

然而, endl(cout); //不起作用

在这个例子中,它是2行对1行。