endl和\n到底有什么区别????
endl和\n好象都是换行吧??
那么他们有什么区别呢
[解决办法]
- C/C++ code
FROM:ostream.h/cpp inline _CRTIMP ostream& __cdecl flush(ostream& _outs) { return _outs.flush(); } inline _CRTIMP ostream& __cdecl endl(ostream& _outs) { return _outs << '\n' << flush; } ostream& ostream::flush() { lock(); lockbuf(); if (bp->sync()==EOF) state |= ios::failbit; unlockbuf(); unlock(); return(*this); }
[解决办法]
输出endl会刷新缓冲区,效率比较低。
所以如果你并不急着看输出结果,尽量使用'\n'。
比如你一次输出了100行,其实刷新一次就够了。多刷了99次是浪费。