logo

C++ Manipulator endl

C++ manipulator endl funktionen används för att infoga ett nytt linjetecken och spola strömmen.

Arbetet med endl-manipulatorn liknar ' '-tecken i C++. Den skriver ut resultatet av följande påstående på nästa rad.

Syntax

 for ostream ostream& endl (ostream& os); basic template template basic_ostream& endl (basic_ostream& os); 

Parameter

du : Utgångsströmobjekt påverkat.

Returvärde

Det returnerar argument du .

Datalopp

Modifierar strömobjektets os.

Om vi ​​försöker få åtkomst samtidigt till samma strömobjekt kan det orsaka datarace, förutom standardströmobjekten cerr, cout, wcout, clog, wcerr och wclog när dessa är synkroniserade med stdio.

Undantag Säkerhet

Objekt du är i ett giltigt tillstånd, om något undantag görs.

Exempel 1

Låt oss se det enkla exemplet för att demonstrera användningen av endl:

 #include using namespace std; int main() { cout << 'Hello' << endl << 'World!'; return 0; } 

Produktion:

mediaöverföring
 Hello World! 

Exempel 2

Låt oss se ett annat enkelt exempel:

 #include using namespace std; int main() { int num; cout&lt;&gt;num; cout&lt;<'hello roll number '<<num<<endl; cout<<'welcome to your new class!!'; return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> Enter your roll number: 22 Hello roll number 22 Welcome to your new class!! </pre> <h2>Example 3</h2> <p>Let&apos;s see another simple example:</p> <pre> #include // std::cout, std::end using namespace std; int main () { int a=100; double b=3.14; cout &lt;&lt; a; cout &lt;&lt; endl; // manipulator inserted alone cout &lt;&lt; b &lt;&lt; endl &lt;&lt; a*b; // manipulator in concatenated insertion endl (cout); // endl called as a regular function return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> 100 3.14 314 </pre> <h2>Example 4</h2> <p>Let&apos;s see another simple example:</p> <pre> #include #include using namespace std; template void log_progress(Diff d) { cout &lt;&lt; chrono::duration_cast(d).count() &lt;&lt; &apos; ms passed&apos; &lt;&lt; endl; } int main() { cout.sync_with_stdio(false); // on some platforms, stdout flushes on 
 volatile int sink = 0; auto t1 = chrono::high_resolution_clock::now(); for (int j=0; j<5; ++j) { for (int n="0;" n<10000; ++n) m="0;" m<20000; ++m) sink +="m*n;" do some work auto now="chrono::high_resolution_clock::now();" log_progress(now - t1); } return 0; < pre> <p> <strong>Output:</strong> </p> <pre> 435 ms passed 894 ms passed 1326 ms passed 1747 ms passed 2178 ms passed </pre></5;></pre></'hello>

Exempel 3

Låt oss se ett annat enkelt exempel:

 #include // std::cout, std::end using namespace std; int main () { int a=100; double b=3.14; cout &lt;&lt; a; cout &lt;&lt; endl; // manipulator inserted alone cout &lt;&lt; b &lt;&lt; endl &lt;&lt; a*b; // manipulator in concatenated insertion endl (cout); // endl called as a regular function return 0; } 

Produktion:

 100 3.14 314 

Exempel 4

Låt oss se ett annat enkelt exempel:

 #include #include using namespace std; template void log_progress(Diff d) { cout &lt;&lt; chrono::duration_cast(d).count() &lt;&lt; &apos; ms passed&apos; &lt;&lt; endl; } int main() { cout.sync_with_stdio(false); // on some platforms, stdout flushes on 
 volatile int sink = 0; auto t1 = chrono::high_resolution_clock::now(); for (int j=0; j<5; ++j) { for (int n="0;" n<10000; ++n) m="0;" m<20000; ++m) sink +="m*n;" do some work auto now="chrono::high_resolution_clock::now();" log_progress(now - t1); } return 0; < pre> <p> <strong>Output:</strong> </p> <pre> 435 ms passed 894 ms passed 1326 ms passed 1747 ms passed 2178 ms passed </pre></5;>