C++ Tutorial/Development/cout

Материал из C\C++ эксперт
Версия от 10:28, 25 мая 2010; Admin (обсуждение | вклад) (1 версия: Импорт контента...)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Содержание

An I/O manipulator: cout

#include <iostream> 
#include <iomanip> 
using namespace std; 
 
int main() 
{ 
  cout << setprecision(2) << 1000.243 << endl; 
 
  return 0; 
}
1e+003

An I/O manipulator: cout

#include <iostream> 
#include <iomanip> 
using namespace std; 
 
int main() 
{ 
  cout << setw(20) << "Hello there."; 
 
  return 0; 
}
Hello there.

Check cout flags

#include<iostream.h>
void showflags();
main()
{
  showflags();
  cout.setf(ios::oct|ios::showbase|ios::fixed);
  showflags();
  return 0;
}
void showflags()
{
  long f;
  char flgs[15][12]={
    "skipws",
       "left",
       "right",
       "internal",
       "dec",
       "oct",
       "hex",
       "showbase",
       "showpoint",
       "uppercase",
       "showpos",
       "scientific",
       "fixed",
       "unitbuf",
       "stdio",
  };
  f=cout.flags();
  for(int i=1,j=0;i<0x4000;i=i<<1,j++)
    if(i&f)
         cout<<flgs[j]<<"is on\n";
       else
         cout<<flgs[j]<<"is off\n";
  cout<<"\n";
}
skipwsis off
leftis on
rightis off
internalis off
decis off
octis off
hexis off
showbaseis off
showpointis off
uppercaseis off
showposis off
scientificis off
fixedis on
unitbufis off
skipwsis off
leftis on
rightis on
internalis off
decis off
octis off
hexis on
showbaseis off
showpointis off
uppercaseis on
showposis off
scientificis off
fixedis on
unitbufis off

cout boolalpha: cause bool values to display as true or false

#include <iostream>
using std::boolalpha;
using std::cout;
using std::endl;
int main()
{
   bool a = true;
   bool b = false;
   cout << boolalpha;
   cout << "a = " << a << "; b = " << b;
   return 0;
}
a = true; b = false

cout: fill()

#include <iostream> 
using namespace std; 
 
int main() 
{ 
  cout.setf(ios::showpos); 
  cout.setf(ios::scientific); 
  cout << 123 << " " << 123.23 << "\n"; 
 
 
  cout.fill("#");  // fill using # 
  cout.width(10);  // in a field of 10 characters 
  cout << 123 << " "; 
  cout.width(10);  // set width to 10 
  cout << 123.23; 
 
  return 0; 
}
+123 +1.232300e+002
######+123 +1.232300e+002

cout: flags() and unsetf()

#include <iostream> 
using namespace std; 
 
int main() 
{ 
  ios::fmtflags f; 
 
  f = cout.flags(); 
 
  if(f & ios::showpos) 
    cout << "showpos is set for cout.\n"; 
  else 
    cout << "showpos is cleared for cout.\n"; 
 
  cout << "\nSetting showpos for cout.\n"; 
  cout.setf(ios::showpos); 
 
  f = cout.flags(); 
 
  if(f & ios::showpos) 
    cout << "showpos is set for cout.\n"; 
  else 
    cout << "showpos is cleared for cout.\n"; 
 
  cout << "\nClearing showpos for cout.\n"; 
  cout.unsetf(ios::showpos); 
 
  f = cout.flags(); 
 
  if(f & ios::showpos) 
    cout << "showpos is set for cout.\n"; 
  else 
    cout << "showpos is cleared for cout.\n"; 
 
  return 0; 
}
showpos is cleared for cout.
Setting showpos for cout.
showpos is set for cout.
Clearing showpos for cout.
showpos is cleared for cout.

cout: precision()

#include <iostream> 
using namespace std; 
 
int main() 
{ 
  cout.setf(ios::showpos); 
  cout.setf(ios::scientific); 
  cout << 123 << " " << 123.23 << "\n"; 
 
  cout.precision(2); // two digits after decimal point 
  cout.width(10);    // in a field of 10 characters 
  cout << 123 << " "; 
  cout.width(10);    // set width to 10 
  cout << 123.23 << "\n"; 
 
  return 0; 
}
+123 +1.232300e+002
      +123 +1.23e+002

cout: setf()

#include <iostream> 
using namespace std; 
 
int main() 
{ 
  // Turn on showpos and scientific flags. 
  cout.setf(ios::showpos); 
  cout.setf(ios::scientific); 
 
  cout << 123 << " " << 123.23 << " "; 
 
  return 0; 
}
+123 +1.232300e+002

cout.setf(ios::showpoint | ios::showpos, ios::showpoint);

#include <iostream>
using namespace std;
int main( )
{
  cout.setf(ios::showpoint | ios::showpos, ios::showpoint);
  cout << 100.0;
  return 0;
}
100.000

cout: setiosflags()

#include <iostream> 
#include <iomanip> 
using namespace std; 
 
int main() 
{ 
  cout << setiosflags(ios::showpos) << setiosflags(ios::scientific) << 123 << 123.23; 
 
  return 0; 
}
+123 +1.232300e+002

cout: unsetf

#include <iostream.h>
main(void)
{
  cout.setf(ios::showpos);
  cout.setf(ios::scientific);
  cout << 123 << "  "<< 123.456 << endl;
  cout.precision(3);
  cout.width(10);
  cout << 123 << "  "<< 123.456 << endl;;
  cout.fill("#");
  cout.width(10);
  cout.unsetf(ios::scientific|ios::showpos);
  cout.precision(5);
  cout << 123 << "  " << 123.456;
  return 0;
}
+123  +1.234560e+002
      +123  +1.235e+002
#######123  123.46

cout: width()

#include <iostream> 
using namespace std; 
 
int main() 
{ 
  cout.setf(ios::showpos); 
  cout.setf(ios::scientific); 
  cout << 123 << " " << 123.23 << "\n"; 
 
  cout.precision(2); // two digits after decimal point 
  cout.width(10);    // in a field of 10 characters 
  cout << 123 << " "; 
  cout.width(10);    // set width to 10 
  cout << 123.23 << "\n"; 
 
  return 0; 
}
+123 +1.232300e+002
      +123 +1.23e+002

Demonstrating left justification and right justification

#include <iostream>
using std::cout;
using std::endl;
using std::left;
using std::right;
#include <iomanip>
using std::setw;
int main()
{
   int x = 12345;
   cout << "Default is right justified:" << endl<< setw( 10 ) << x;
   cout << "\n\nUse std::left to left justify x:\n"<< left << setw( 10 ) << x;

   cout << "\n\nUse std::right to right justify x:\n"<< right << setw( 10 ) << x << endl;
   return 0;
}
Default is right justified:
     12345
Use std::left to left justify x:
12345
Use std::right to right justify x:
     12345

Demonstrating stream-manipulators boolalpha and noboolalpha.

#include <iostream>
using std::boolalpha;
using std::cout;
using std::endl;
using std::noboolalpha;
int main()
{
   bool booleanValue = true;
   cout << "booleanValue is " << booleanValue << endl;
   cout << "booleanValue (after using boolalpha) is "<< boolalpha << booleanValue << endl << endl;
   booleanValue = false; // change booleanValue
   cout << noboolalpha << endl; // use noboolalpha
   cout << "booleanValue is " << booleanValue << endl;
   cout << "booleanValue (after using boolalpha) is " << boolalpha << booleanValue << endl;
   return 0;
}
booleanValue is 1
booleanValue (after using boolalpha) is true

booleanValue is 0
booleanValue (after using boolalpha) is false

Demonstrating the flags member function

#include <iostream>
using std::cout;
using std::endl;
using std::ios_base;
using std::oct;
using std::scientific;
using std::showbase;
int main()
{
   int integerValue = 1000;
   double doubleValue = 0.0947628;
   cout << cout.flags() << "\n\n" << integerValue << "\t" << doubleValue << endl << endl;
   ios_base::fmtflags originalFormat = cout.flags();
   cout << showbase << oct << scientific;
   cout << cout.flags() << "\n\n" << integerValue << "\t" << doubleValue << endl << endl;
   cout.flags( originalFormat );
   cout << cout.flags() << "\n\n" << integerValue << "\t" << doubleValue << endl;
   return 0;
}
4098
1000    0.0947628
011500
01750   9.476280e-002
4098
1000    0.0947628

Fixed-point mode, show a "+" for positive nums, show 3 digits to the right of the decimal

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main( ) {
   ios_base::fmtflags flags = cout.flags( );
   double pi = 3.14285714;
   cout << "pi = " << fixed            
        << showpos                     
        << setprecision(3)
        << pi << "\n";    
   cout.flags(flags);
}
pi = +3.143

ios::showpos | ios::showbase | ios::oct | ios::right

#include <iostream>
using namespace std;
void showflags();
int main()
{
  // show default condition of format flags
  showflags();
  // showpos, showbase, oct, right are on, others off
  ios::fmtflags f = ios::showpos | ios::showbase | ios::oct | ios::right;
  cout.flags(f);
  showflags();
  return 0;
}

void showflags()
{
  ios::fmtflags f;
  long i;
  f = cout.flags(); // get flag settings
  // check each flag
  for(i=0x4000; i; i = i >> 1)
    if(i & f) cout << "1 ";
    else cout << "0 ";
  cout << " \n";
}
0 0 1 0 0 0 0 0 0 0 0 0 0 1 0
0 0 0 1 0 1 0 1 1 0 0 0 0 0 0

OR together two or more flags

#include <iostream>
using namespace std;
int main()
{
  cout.setf(ios::showpoint | ios::showpos);
  cout << 100.0; 
  return 0;
}
+100.000

Output a pointer

#include <iostream> 
using namespace std; 
 
int main() 
{ 
  char *ptr; 
 
  ptr = "Pointers add power to C++.\n"; 
 
  cout << ptr; 
 
  return 0; 
}
Pointers add power to C++.

parameterless manipulators that operate on output streams

Manipulator            Purpose
boolalpha              Turns on boolalpha flag.
endl                   Outputs a newline.
ends                   Outputs a null.
dec                    Turns on dec flag. Turns off the hex and oct flags.
fixed                  Turns on fixed flag. Turns off the scientific flag.
flush                  Flushes the stream.
hex                    Turns on hex flag. Turns off the dec and oct flags.
internal               Turns on internal flag. Turns off the left and right flags.
left                   Turns on left flag. Turns off the right and internal flags.
noboolalpha            Turns off boolalpha flag.
noshowbase             Turns off showbase flag.
noshowpoint            Turns off showpoint flag.
noshowpos              Turns off showpos flag.
nounitbuf              Turns off unitbuf flag.
nouppercase            Turns off uppercase flag.
oct                    Turns on oct flag. Turns off the dec and hex flags.
right                  Turns on right flag. Turns off the left and internal flags.
scientific             Turns on scientific flag. Turns off the fixed flag.
showbase               Turns on showbase flag.
showpoint              Turns on showpoint flag.
showpos                Turns on showpos flag.
unitbuf                Turns on unitbuf flag.
uppercase              Turns on uppercase flag.
   
#include <iostream>
using namespace std; 
void showflags(void);
int main(void)
{
   showflags();
   cout.setf(ios::right | ios::showpoint | ios::fixed);
   showflags();
}
void showflags(void){
   long flag_set, i;
   int j;
   char flags[15][12] = {
      "skipws", "left", "right", "internal", "dec",
      "oct", "hex", "showbase", "showpoint", "uppercase",
      "showpos", "scientific", "fixed", "unitbuf",
   };
   flag_set = cout.flags();
   for (i=1, j=0; i<0x2000; i = i<<1, j++)
      if (i & flag_set)
         cout << flags[j] << " is on." << endl;
      else
         cout << flags[j] << " is off." << endl;
   cout << endl;
}

Printing a line of text with multiple statements

#include <iostream> 
int main()
{
   std::cout << "Welcome ";
   std::cout << "to C++!\n";
   return 0;
}
Welcome to C++!

Printing an integer with internal spacing and plus sign

#include <iostream>
using std::cout;
using std::endl;
using std::internal;
using std::showpos;
#include <iomanip>
using std::setw;
int main()
{
   cout << internal << showpos << setw( 10 ) << 123 << endl;
   return 0;
}
+      123

Printing multiple lines of text with a single statement: use the \n

#include <iostream>
int main()
{
   std::cout << "Welcome\nto\n\nC++!\n";
   return 0;
}
Welcome
to
C++!

Scientific mode; don"t show plus sign anymore

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main( ) {
   ios_base::fmtflags flags = cout.flags( );
   double pi = 3.14285714;
   cout << "pi = " << scientific
        << noshowpos
        << pi << "\n";
   cout.flags(flags);
}
pi = 3.142857e+000

Set cout to output hex

#include <iostream>
using namespace std;
int main()
{
  cout.setf(ios::hex, ios::basefield);
  cout << 100; // this displays 64
  return 0;
}
64

setiosflags(ios::showpos); and setiosflags(ios::showbase);

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
  cout << setiosflags(ios::showpos);
  cout << setiosflags(ios::showbase);
  cout << 123 << " " << hex << 123;
  return 0;
}
+123 0x7b

Set ios::showpoint, ios::showpos for double pointer number

#include <iostream>
using namespace std;
int main()
{
  cout.setf(ios::showpoint);
  cout.setf(ios::showpos);
  cout << 100.0; 
  return 0;
}
+100.000

Show default condition of format flags

#include <iostream>
using namespace std;
void showflags() ;
int main()
{
  
  showflags();
  cout.setf(ios::right | ios::showpoint | ios::fixed);
  showflags();
  return 0;
}
void showflags()
{
  ios::fmtflags f;
  long i;
  f = cout.flags(); // get flag settings
  // check each flag
  for(i=0x4000; i; i = i >> 1)
    if(i & f) cout << "1 ";
    else cout << "0 ";
  cout << " \n";
}
0 0 1 0 0 0 0 0 0 0 0 0 0 1 0
0 0 1 0 1 0 0 1 0 0 0 0 1 1 0

Skip leading whitespace.

#include <iostream> 
using namespace std; 
 
int main() 
{ 
  char s[80]; 
 
  cin >> ws >> s; 
  cout << s; 
 
  return 0; 
}
string string
string

Stream-manipulator uppercase.

#include <iostream>
using std::cout;
using std::endl;
using std::hex;
using std::showbase;
using std::uppercase;
int main()
{
   cout << uppercase << 4.345e10 << endl << hex << showbase << 123456789  << endl;
   return 0;
}
4.345E+010
0X75BCD15

To use a parameterized manipulator, you must include <iomanip>. It defines the following manipulators:

resetiosflags (ios_base::fmtflags f)       Turn off the flags specified in f.
setbase(int base)                          Set the number base to base.
setfill(int ch)                            Set the fill character to ch.
setiosflags(ios_base::fmtflags f)          Turn on the flags specified in f.
setprecision (int p)                       Set the number of digits of precision.
setw(int w)                                Set the field width to w.

unset flag one by one

#include <iostream>
using namespace std;
int main()
{
  cout.setf(ios::uppercase | ios::scientific);
  cout << 100.12;
  cout.unsetf(ios::uppercase);
  cout << " \n" << 100.12;
  return 0;
}
1.001200E+002
1.001200e+002"

Use function in cout statement

#include <iostream> 
using namespace std; 
int box(int length, int width, int height); // return the volume 
int main() 
{ 
  // use the return value of box( ) directly 
  cout << "The volume is " <<  box(10.1, 11.2, 3.3); 
 
  return 0; 
}
// This function returns a value.  
int box(int length, int width, int height) 
{ 
  return length * width * height ; 
}
The volume is 330

Use std::cout to output message to console

#include <iostream>
 int main()
 {
     std::cout << "Hello World!\n";
     return 0;
 }
Hello World!

Use std::cout to output various data

#include <iostream>
int main()
{
    std::cout << "test.\n";
    std::cout << "Here is 5: " << 5 << "\n";
    std::cout << "Here is a very big number:\t" << 70000;
    std::cout << (float) 5/8 << std::endl;
    std::cout << (double) 7000 * 7000 << std::endl;
    std::cout << "a liiiiiiiiiiiiiiiiiiiiiiine\n";
    return 0;
}
test.
Here is 5: 5
Here is a very big number:      700000.625
4.9e+007
a liiiiiiiiiiiiiiiiiiiiiiine

Use std::endl to output a new line sign

#include <iostream>
int main() {
  int x = 5;
  int y = 7;
  std::cout << std::endl;
  std::cout << x + y << " " << x * y;
  std::cout << std::endl;
  return 0;
}
12 35

use the setiosflags( ) manipulator to directly set the various format flags related to a stream.

#include <iostream>
#include <iomanip>
using namespace std;
   
int main()
{
  cout << setiosflags(ios::showpos);
  cout << setiosflags(ios::showbase);
  cout << 123 << " " << hex << 123;
   
  return 0;
}

Using Manipulators to Format I/O

/*
Manipulator                   Purpose                             Input/Output
boolalpha                     Turns on boolapha flag.             Input/Output
dec                           Turns on dec flag.                  Input/Output
endl                          Output a newline character and flush the stream.   Output
ends                          Output a null.                      Output
fixed                         Turns on fixed flag.                Output
flush                         Flush a stream.                     Output
hex                           Turns on hex flag.                  Input/Output
internal                      Turns on internal flag.             Output
left                          Turns on left flag.                 Output
nobooalpha                    Turns off boolalpha flag.           Input/Output
noshowbase                    Turns off showbase flag.            Output
noshowpoint                   Turns off showpoint flag.           Output
noshowpos                     Turns off showpos flag.             Output
noskipws                      Turns off skipws flag.              Input
nounitbuf                     Turns off unitbuf flag.             Output
nouppercase                   Turns off uppercase flag.           Output
oct                           Turns on oct flag.                  Input/Output
resetiosflags (fmtflags f)    Turn off the flags specified in f.  Input/Output
right                         Turns on right flag.                Output
scientific                    Turns on scientific flag.           Output
setbase(int base)             Set the number base to base.        Input/Output
setfill(int ch)               Set the fill character to ch.       Output
setiosflags(fmtflags f)       Turn on the flags specified in f.   Input/output
setprecision (int p)          Set the number of digits of precision. Output
setw(int w)                   Set the field width to w.           Output
showbase                      Turns on showbase flag.             Output
showpoint                     Turns on showpoint flag.            Output
showpos                       Turns on showpos flag.              Output
skipws                        Turns on skipws flag.               Input
unitbuf                       Turns on unitbuf flag.              Output
uppercase                     Turns on uppercase flag.            Output
ws                            Skip leading white space.           Input
*/

#include <iostream>
#include <iomanip>
using namespace std;
   
int main()
{
  cout << hex << 100 << endl;
   
  cout << setfill("?") << setw(10) << 2343.0;
   
  return 0;
}

Using showpoint to control the printing of trailing zeros anddecimal points for doubles

#include <iostream>
using std::cout;
using std::endl;
using std::showpoint;
int main()
{
   cout << "Before using showpoint" << endl
      << "9.9900 prints as: " << 9.9900 << endl
      << "9.9000 prints as: " << 9.9000 << endl
      << "9.0000 prints as: " << 9.0000 << endl << endl;
   cout << showpoint  
      << "After using showpoint" << endl
      << "9.9900 prints as: " << 9.9900 << endl
      << "9.9000 prints as: " << 9.9000 << endl
      << "9.0000 prints as: " << 9.0000 << endl;
   return 0;
}
Before using showpoint
9.9900 prints as: 9.99
9.9000 prints as: 9.9
9.0000 prints as: 9
After using showpoint
9.9900 prints as: 9.99000
9.9000 prints as: 9.90000
9.0000 prints as: 9.00000

Using stream-manipulator showbase to show number base

#include <iostream>
using std::cout;
using std::endl;
using std::hex;
using std::oct;
using std::showbase;
int main()
{
   int x = 100;
   cout << "Printing integers preceded by their base:" << endl << showbase;
   cout << x << endl; // print decimal value
   cout << oct << x << endl; // print octal value
   cout << hex << x << endl; // print hexadecimal value
   return 0;
}
Printing integers preceded by their base:
100
0144
0x64

Using write().

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
   char One[] = "this is a test";
   int fullLength = strlen(One);
   int tooShort = fullLength -4;
   int tooLong = fullLength + 6;
   cout.write(One,fullLength) << "\n";
   cout.write(One,tooShort) << "\n";
   cout.write(One,tooLong) << "\n";
   return 0;
}