Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/io/manip/endl"

From cppreference.com
< cpp‎ | io‎ | manip
m (r2.7.3) (Robot: Adding de, es, fr, it, ja, pt, ru, zh)
m (Fix C++ Core Guidelines link)
 
(13 intermediate revisions by 8 users not shown)
Line 1: Line 1:
 
{{cpp/title|endl}}
 
{{cpp/title|endl}}
 
{{cpp/io/manip/navbar}}
 
{{cpp/io/manip/navbar}}
{{ddcl | header=ostream |
+
{{ddcl|header=ostream|
 
template< class CharT, class Traits >
 
template< class CharT, class Traits >
std::basic_ostream<charT,traits>& endl( std::basic_ostream<CharT, Traits>& os );
+
std::basic_ostream<CharT, Traits>& endl( std::basic_ostream<CharT, Traits>& os );
 
}}
 
}}
  
Inserts a endline character into the output sequence {{tt|os}} and flushes it as if by calling {{c|os.put(os.widen('\n'))}} followed by {{c|os.flush()}}.  
+
Inserts a newline character into the output sequence {{c|os}} and flushes it as if by calling {{c|os.put(os.widen('\n'))}} followed by {{c|os.flush()}}.  
  
This is an output-only I/O manipulator, it may be called with an expression such as {{c|out << std::endl}} for any {{tt|out}} of type {{c|std::basic_ostream}}.
+
This is an output-only I/O manipulator, it may be called with an expression such as {{c|out << std::endl}} for any {{tt|out}} of type {{lc|std::basic_ostream}}.
  
 
===Notes===
 
===Notes===
This manipulator may be used to produce a line of output immediately, e.g. when displaying output from a long-running process, logging activity of multiple threads or logging activity of a program that may crash unexpectedly. An explicit flush  of {{c|std::cout}} is also necessary before a call to {{c|std::system}}, if the spawned process performs any screen I/O (a common example is {{c|std::system("pause")}} on Windows). In most other usual interactive I/O scenarios, {{c|std::endl}} is redundant when used with {{c|std::cout}} because any input from {{c|std::cin}}, output to {{c|std::cerr}}, or program termination forces a call to {{c|std::cout.flush()}}. Use of {{c|std::endl}} in place of {{c|'\n'}}, encouraged by some sources, may significantly degrade output performance.
+
This manipulator may be used to produce a line of output immediately, e.g. when displaying output from a long-running process, logging activity of multiple threads or logging activity of a program that may crash unexpectedly. An explicit flush  of {{lc|std::cout}} is also necessary before a call to {{lc|std::system}}, if the spawned process performs any screen I/O.<!--couldn't reproduce in VS2013, did it change? ---- (a common example is {{c|std::system("pause")}} on Windows).--> In most other usual interactive I/O scenarios, {{tt|std::endl}} is redundant when used with {{lc|std::cout}} because any input from {{lc|std::cin}}, output to {{lc|std::cerr}}, or program termination forces a call to {{c|std::cout.flush()}}. Use of {{tt|std::endl}} in place of {{c/core|'\n'}}, encouraged by some sources, may significantly degrade output performance.
  
In many implementations, standard output is line-buffered, and writing {{c|'\n'}} causes a flush anyway, unless {{c|std::cout.sync_with_stdio(false)}} was executed. In those situations, unnecessary {{tt|endl}} only degrades the performance of file output, not standard output.
+
In many implementations, standard output is line-buffered, and writing {{c/core|'\n'}} causes a flush anyway, unless {{c|std::ios::sync_with_stdio(false)}} was executed. In those situations, unnecessary {{tt|endl}} only degrades the performance of file output, not standard output.
  
The code samples on this wiki [http://www2.research.att.com/~bs/3rd_code.html follow Bjarne Stroustrup] in flushing the standard output only where necessary.
+
The code samples on this wiki [https://www.stroustrup.com/3rd_code.html follow Bjarne Stroustrup] and [https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rio-endl The C++ Core Guidelines] in flushing the standard output only where necessary.
  
When an incomplete line of output needs to be flushed, the {{c|std::flush}} manipulator may be used.
+
When an incomplete line of output needs to be flushed, the {{lc|std::flush}} manipulator may be used.
  
When every character of output needs to be flushed, the {{c|std::unitbuf}} manipulator may be used.
+
When every character of output needs to be flushed, the {{lc|std::unitbuf}} manipulator may be used.
  
 
===Parameters===
 
===Parameters===
{{param list begin}}
+
{{par begin}}
{{param list item | os | reference to output stream }}
+
{{par|os|reference to output stream}}
{{param list end}}
+
{{par end}}
  
 
===Return value===
 
===Return value===
 
+
{{c|os}} (reference to the stream after manipulation).
{{tt|os}} (reference to the stream after manipulation)
+
  
 
===Example===
 
===Example===
 
{{example
 
{{example
| With \n instead of endl, the output would be the same, but may not appear in real time.
+
|With {{c/core|'\n'}} instead of {{tt|endl}}, the output would be the same, but may not appear in real time.
| code=
+
|code=
 +
#include <chrono>
 
#include <iostream>
 
#include <iostream>
#include <chrono>
+
 
template<typename Diff>
 
template<typename Diff>
 
void log_progress(Diff d)
 
void log_progress(Diff d)
 
{
 
{
     std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(d).count()
+
     std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(d)
               << " ms passed" << std::endl;
+
               << " passed" << std::endl;
 
}
 
}
 +
 
int main()
 
int main()
 
{
 
{
 
     std::cout.sync_with_stdio(false); // on some platforms, stdout flushes on \n
 
     std::cout.sync_with_stdio(false); // on some platforms, stdout flushes on \n
     volatile int sink=0;
+
 
+
     static volatile int sink{};
     auto t1 = std::chrono::high_resolution_clock::now();
+
     const auto t1 = std::chrono::high_resolution_clock::now();
     for(int j=0; j<5; ++j)
+
     for (int i = 0; i < 5; ++i)
 
     {
 
     {
         for(int n=0; n<10000; ++n)
+
         for (int j = 0; j < 10000; ++j)
             for(int m=0; m<20000; ++m)
+
             for (int k = 0; k < 20000; ++k)
                 sink += m*n; // do some work
+
                 sink += i * j * k; // do some work
         auto now = std::chrono::high_resolution_clock::now();
+
         log_progress(std::chrono::high_resolution_clock::now() - t1);
        log_progress(now - t1);
+
 
     }
 
     }
 
}
 
}
| output=
+
|p=true
487 ms passed
+
|output=
974 ms passed
+
566ms passed
1470 ms passed
+
1133ms passed
1965 ms passed
+
1699ms passed
2455 ms passed
+
2262ms passed
 +
2829ms passed
 
}}
 
}}
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/io/manip/dcl list unitbuf}}
+
{{dsc inc|cpp/io/manip/dsc unitbuf}}
{{dcl list template | cpp/io/manip/dcl list flush}}
+
{{dsc inc|cpp/io/manip/dsc flush}}
{{dcl list template | cpp/io/basic_ostream/dcl list flush | mem=std::basic_ostream}}
+
{{dsc inc|cpp/io/basic_ostream/dsc flush}}
{{dcl list end}}
+
{{dsc end}}
  
[[de:cpp/io/manip/endl]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/io/manip/endl]]
+
[[fr:cpp/io/manip/endl]]
+
[[it:cpp/io/manip/endl]]
+
[[ja:cpp/io/manip/endl]]
+
[[pt:cpp/io/manip/endl]]
+
[[ru:cpp/io/manip/endl]]
+
[[zh:cpp/io/manip/endl]]
+

Latest revision as of 16:43, 23 September 2023

 
 
 
Input/output manipulators
Floating-point formatting
Integer formatting
Boolean formatting
Field width and fill control
Other formatting
Whitespace processing
Output flushing
endl
(C++20)  

Status flags manipulation
Time and money I/O
(C++11)
(C++11)
(C++11)
(C++11)
Quoted manipulator
(C++14)
 
Defined in header <ostream>
template< class CharT, class Traits >
std::basic_ostream<CharT, Traits>& endl( std::basic_ostream<CharT, Traits>& os );

Inserts a newline character into the output sequence os and flushes it as if by calling os.put(os.widen('\n')) followed by os.flush().

This is an output-only I/O manipulator, it may be called with an expression such as out << std::endl for any out of type std::basic_ostream.

Contents

[edit] Notes

This manipulator may be used to produce a line of output immediately, e.g. when displaying output from a long-running process, logging activity of multiple threads or logging activity of a program that may crash unexpectedly. An explicit flush of std::cout is also necessary before a call to std::system, if the spawned process performs any screen I/O. In most other usual interactive I/O scenarios, std::endl is redundant when used with std::cout because any input from std::cin, output to std::cerr, or program termination forces a call to std::cout.flush(). Use of std::endl in place of '\n', encouraged by some sources, may significantly degrade output performance.

In many implementations, standard output is line-buffered, and writing '\n' causes a flush anyway, unless std::ios::sync_with_stdio(false) was executed. In those situations, unnecessary endl only degrades the performance of file output, not standard output.

The code samples on this wiki follow Bjarne Stroustrup and The C++ Core Guidelines in flushing the standard output only where necessary.

When an incomplete line of output needs to be flushed, the std::flush manipulator may be used.

When every character of output needs to be flushed, the std::unitbuf manipulator may be used.

[edit] Parameters

os - reference to output stream

[edit] Return value

os (reference to the stream after manipulation).

[edit] Example

With '\n' instead of endl, the output would be the same, but may not appear in real time.

#include <chrono>
#include <iostream>
 
template<typename Diff>
void log_progress(Diff d)
{
    std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(d)
              << " passed" << std::endl;
}
 
int main()
{
    std::cout.sync_with_stdio(false); // on some platforms, stdout flushes on \n
 
    static volatile int sink{};
    const auto t1 = std::chrono::high_resolution_clock::now();
    for (int i = 0; i < 5; ++i)
    {
        for (int j = 0; j < 10000; ++j)
            for (int k = 0; k < 20000; ++k)
                sink += i * j * k; // do some work
        log_progress(std::chrono::high_resolution_clock::now() - t1);
    }
}

Possible output:

566ms passed
1133ms passed
1699ms passed
2262ms passed
2829ms passed

[edit] See also

controls whether output is flushed after each operation
(function) [edit]
flushes the output stream
(function template) [edit]
synchronizes with the underlying storage device
(public member function of std::basic_ostream<CharT,Traits>) [edit]