这可能是一个初学者的问题,了解cout如何工作可能是关键.如果有人可以联系到一个好的解释,那就太好了.
cout<<cout并cout<<&cout在linux x86机器上打印以4分隔的十六进制值.
在这个Stack Overflow回答中,
它表示与之std::cout << "Hello World!" << std::endl;相同
std::operator<<(std::operator<<(std::cout, "Hello World!"), std::endl);
Run Code Online (Sandbox Code Playgroud)
但是当我编译上面的代码行时,它不会编译!然后尝试别的东西后,我发现,它不会编译的原因是因为std::endl,如果我取代std::endl的"\n",然后它工作.但为什么你不能传递std::endl给std::operator<<?
或者更简单,是不是std::cout<<std::endl;一样std::operator<<(std::cout, std::endl);?
编辑
编译时icpc test.cpp,错误消息是
error: no instance of overloaded function "std::operator<<" matches the argument list argument types are: (std::ostream, <unknown-type>) std::operator<<(std::cout, std::endl);
并g++ test.cpp提供更长的错误消息.
我读了这一行,我不明白它的作用:
if(cout<<X) //What does this mean?
{
...
}
Run Code Online (Sandbox Code Playgroud) 这是我发现但当我使用"cout <时我无法理解一个地址
#include<iostream>
using namespace std;
int main()
{
char a[2]={'a','b'};
char b[3]="ab";
cout<<&a<<endl;
cout<<&b<<endl;
cout<<sizeof(a)<<endl<<cout<<sizeof(b);//the result of this I am puzzled
return 0;
}
Run Code Online (Sandbox Code Playgroud)
结果是:
0x28ff2e
0x28ff10
2
0x4453c43
Run Code Online (Sandbox Code Playgroud) #include <iostream>
using namespace std;
int main()
{
if (!(cout << "geeks"))
cout <<" geeks ";
else
cout << "forgeeks ";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么cout << "geeks";在if条件中执行?我知道if语句是假的.我"forgeeks "只期待.