logo

Manipulador C++ endl

C++ manipulador endl La funció s'utilitza per inserir un caràcter de línia nou i esborrar el flux.

El funcionament del manipulador endl és similar al caràcter ' ' en C++. Imprimeix la sortida de la instrucció següent a la línia següent.

Sintaxi

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

Paràmetre

vostè : objecte del flux de sortida afectat.

Valor de retorn

Retorna argument vostè .

Carreres de dades

Modifica el sistema operatiu de l'objecte de flux.

Si intentem accedir simultàniament al mateix objecte de flux, pot provocar curses de dades, excepte els objectes de flux estàndard cerr, cout, wcout, clog, wcerr i wclog quan aquests estan sincronitzats amb stdio.

Seguretat d'excepció

Objecte vostè està en un estat vàlid, si es llança alguna excepció.

Exemple 1

Vegem l'exemple senzill per demostrar l'ús de endl:

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

Sortida:

 Hello World! 

Exemple 2

Vegem un altre exemple senzill:

 #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>

Exemple 3

Vegem un altre exemple senzill:

 #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; } 

Sortida:

 100 3.14 314 

Exemple 4

Vegem un altre exemple senzill:

 #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;>