c++ endl code example

Example 1: C++ and endl

// endl example
#include <iostream>     // std::cout, std::end
using namespace std;
int main () {

  int a=100;
  double b=3.14;

  cout << a;
  cout << endl;              // manipulator inserted alone
  cout << b << endl << a*b;  // manipulator in concatenated insertion
  endl (cout);               // endl called as a regular function

  return 0;
}

Example 2: std ::endl

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

Example 3: c++ endl

cout << "Hello " << '\n'<<"world";
cout << "Hello " << endl<<"world";
//Hello  
//world

Tags:

Cpp Example