C++/Console/cout fill

Материал из C\C++ эксперт
Перейти к: навигация, поиск

cout fill(): Specify the fill character

<source lang="cpp">

  1. include <iostream>

using namespace std; int main() {

 cout.precision(4) ;
 cout.width(10);
 cout << 10.12345 << "\n";
 cout.fill("*");
 cout.width(10);
 cout << 10.12345 << "\n";
 
 cout.width(10);
 cout << "Hi!" << "\n"; 
 cout.width(10);
 cout.setf(ios::left); 
 cout << 10.12345; 
 return 0;

}


 </source>


Set fill() for string output

<source lang="cpp">

  1. include <iostream>

using namespace std; int main() {

 cout << "Hello" << endl;
 // Now set the width and the fill character.
 cout.width(10);
 cout.fill("*");
 cout << "Hello" << endl;
 return 0;

}


 </source>


Uses ios member functions: setf, ios::hex, ios::basefield

<source lang="cpp">

  1. include <iostream>
  2. include <iomanip>

using namespace std; int main() {

 cout.setf(ios::hex, ios::basefield);
 cout << 12300 << "\n";
 cout.fill("?");
 cout.width(10);
 cout << 232343.0;
 return 0;

}


 </source>


Using fill() and width for string output

<source lang="cpp">

  1. include <iostream>

using namespace std; int main() {

  cout << "Start >";
  cout.width(25);
  cout << 123 << "< End\n";
  cout << "Start >";
  cout.width(25);
  cout.fill("*");
  cout << 123 << "< End\n";
return 0;

}


 </source>


Using fill() and width setting for string output

<source lang="cpp">

  1. include <iostream>

using namespace std; int main() {

  cout << "Start >";
  cout.width(25);
  cout << 123 << "< End\n";
  cout << "Start >";
  cout.width(25);
  cout.fill("*");
  cout << 123 << "< End\n";
  cout << "Start >";
  cout.width(25);
  cout << 456 << "< End\n";
  return 0;

}


 </source>


Using fill() to set filler for extra space

<source lang="cpp">

  1. include <iostream>

using namespace std; int main() {

  cout << "Start >";
  cout.width(25);
  cout << 123 << "< End\n";
  cout << "Start >";
  cout.width(25);
  cout.fill("*");
  cout << 123 << "< End\n";
  return 0;

}


 </source>