endl c++ 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: stoi c++

// stoi example
#include <iostream>   // std::cout
#include <string>     // std::string, std::stoi

int main ()
{
  std::string str_dec = "2001, A Space Odyssey";
  std::string str_hex = "40c3";
  std::string str_bin = "-10010110001";
  std::string str_auto = "0x7f";

  std::string::size_type sz;   // alias of size_t

  int i_dec = std::stoi (str_dec,&sz);
  int i_hex = std::stoi (str_hex,nullptr,16);
  int i_bin = std::stoi (str_bin,nullptr,2);
  int i_auto = std::stoi (str_auto,nullptr,0);

  std::cout << str_dec << ": " << i_dec << " and [" << str_dec.substr(sz) << "]\n";
  std::cout << str_hex << ": " << i_hex << '\n';
  std::cout << str_bin << ": " << i_bin << '\n';
  std::cout << str_auto << ": " << i_auto << '\n';

  return 0;
}

Example 3: c++ endl

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

Tags:

Cpp Example