“std::endl” vs “\n”

后端 未结 12 2376
耶瑟儿~
耶瑟儿~ 2020-11-21 04:35

Many C++ books contain example code like this...

std::cout << \"Test line\" << std::endl;

...so I\'ve always done that too. But

12条回答
  •  孤街浪徒
    2020-11-21 05:14

    There's another function call implied in there if you're going to use std::endl

    a) std::cout << "Hello\n";
    b) std::cout << "Hello" << std::endl;
    

    a) calls operator << once.
    b) calls operator << twice.

提交回复
热议问题